Reputation: 163
I want to force UITextField input to uppercased text. This is what I did:
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
let nsString = textField.text as NSString?
let newString = nsString?.replacingCharacters(in: range, with: string)
textField.text = newString?.uppercased()
return false
}
Is there any way to force uppercase UITextField without using replacingCharacterInRange
of NSString
?
Upvotes: 1
Views: 2731
Reputation: 163
I found it!
I just added a target to my TextField of type UIControlEvents.editingChanged
And in the selector I did: textField.text = textField.text?.uppercased()
Here is the code:
myTextField.addTarget(self, action: #selector(myTextFieldTextChanged), for: UIControlEvents.editingChanged)
func myTextFieldTextChanged (textField: UITextField) {
textField.text = textField.text?.uppercased()
}
This is exactly what I was looking for. Thanks every one!
Upvotes: 5
Reputation: 588
It's very easy, just do this
self.yourTexField.autocapitalizationType = UITextAutocapitalizationType.AllCharacters;
Upvotes: 2