I know it's an old question, but here it is:
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
if !["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"].contains(string.lowercased()) {
return false
}
return true
}
That's a UITextFieldDelegate
method, so you need to make your View Controller conform to it. You also need to set your textfield's delegate to self, like so:
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
myTextField.delegate = self
}
What it does, is to check if each letter the user types belong to the list of letters above... if it doesn't, then it doesn't allow the textfield to change.