UITextfield keyboard with only alphabet, no numbers, no caps, no spacebar?

前端 未结 8 1946
深忆病人
深忆病人 2021-02-06 06:33

I want the keyboard for the UITextfield to only have a-z, no numbers, no special characters (!@$!@$@!#), and no caps. Basicly I am going for a keyboard with only the alphabet.

8条回答
  •  一整个雨季
    2021-02-06 07:05

    Update for those who wants to Allow Space, Caps & Backspace Only

    Swift 4.x, Swift 5.x & up

    func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
    
        if range.location == 0 && string == " " { // prevent space on first character
            return false
        }
    
        if textField.text?.last == " " && string == " " { // allowed only single space
            return false
        }
    
        if string == " " { return true } // now allowing space between name
    
        if string.rangeOfCharacter(from: CharacterSet.letters.inverted) != nil {
            return false
        }
    
        return true
    }
    

提交回复
热议问题