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

前端 未结 8 2065
深忆病人
深忆病人 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 06:53

    class ViewController: UIViewController,UITextFieldDelegate {  
    
         @IBOutlet var phoneTextField:UITextField!
    
        override func viewDidLoad() {
                super.viewDidLoad() 
        self.phoneTextField.delegate = self
        }
    
         func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool
               {
                    let textString = (textField.text! as NSString).replacingCharacters(in: range, with: string)
    
                    if textField == self.phoneTextField  && string.characters.count > 0 {
                        let LettersOnly = NSCharacterSet.Letters
                        let strValid = LettersOnly.contains(UnicodeScalar.init(string)!)
                        return strValid && textString.characters.count <= 10
                    }
                    return true
                }
        }
    

    Try this code
    In above code is only allow 10 char in text field

提交回复
热议问题