Uppercase characters in UItextfield

前端 未结 17 1315
萌比男神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:23

    For those looking for a Swift version.

    Swift 4

    func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
        textField.text = (textField.text! as NSString).replacingCharacters(in: range, with: string.uppercased())
    
        return false
    }
    

    Original answer

    func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
        textField.text = (textField.text as NSString).stringByReplacingCharactersInRange(range, withString: string.uppercaseString)
    
        return false
    }
    

    Using the Capitalization: All Characters property just forces keyboard to open with caps lock on, but lets the user to turned it off.

提交回复
热议问题