Uppercase characters in UItextfield

前端 未结 17 1275
萌比男神i
萌比男神i 2020-12-24 04:34

I have a question about iOS UIKeyboard.

I have a UITextField and I would to have the keyboard with only uppercase characters.<

17条回答
  •  死守一世寂寞
    2020-12-24 05:32

    This is a different approach I used, where it does the following:

    1. Enforces capitalization as soon as the character is entered
    2. Catches situations where the user disables caps lock even if it textfield is set to auto caps
    3. Allows for easy editing
    4. Works with Swift 2.2

    First, register a notification to be updated whenever any changes occur in the textfield.

        textField.addTarget(self, action: #selector(YourClassName.textFieldDidChange(_:)), forControlEvents: UIControlEvents.EditingChanged)
    

    Then, implement textFieldDidChange.

    func textFieldDidChange(textField: UITextField) {
        textField.text = textField.text?.uppercaseString
    }
    

    I chose this to avoid a situation where the user sees an uneven experience of some capitalized, but then changed once they move to the next character.

提交回复
热议问题