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

前端 未结 8 1956
深忆病人
深忆病人 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:12

    all the answers is working when user is not copy and paste in textfield for copy and paste to work use blow code

    func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
    
        let text = (textField.text! as NSString).replacingCharacters(in: range, with: string)
        let Regex = "[a-z A-Z ]+"
        let predicate = NSPredicate.init(format: "SELF MATCHES %@", Regex)
        if predicate.evaluate(with: text) || string == ""
        {
            return true
        }
        else
        {
            return false
        }
    
    }
    
    0 讨论(0)
  • 2021-02-06 07:12

    Swift 3 with spaces

    func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
        if string == " " { return true }
        if let _ = string.rangeOfCharacter(from: CharacterSet.letters){
            return true
        }
        return false
    }
    
    0 讨论(0)
提交回复
热议问题