iPhone Force Textbox Input to Upper Case

前端 未结 11 1629
无人共我
无人共我 2020-12-15 03:01

How do I force characters input into a textbox on the iPhone to upper case?

11条回答
  •  轮回少年
    2020-12-15 03:28

    Maybe I haven't found all the kinks to this method, but so far it works well for me. It seems to avoid the issues with jumping selection to the end if you go down the route of returning NO from -textField:shouldChangeCharactersInRange:replacementString:. Since UITextField conforms to UIKeyInput, simply override the method -insertText: to change the input string to uppercase before calling super with the uppercase string:

    - (void)insertText:(NSString *)text {
        NSString *uppercaseText = [text uppercaseString]
        [super insertText:uppercaseText]
    }
    

    Or if you've moved along to Swift:

    override func insertText(text: String) {
        let uppercaseText = text.uppercaseString
        super.insertText(uppercaseText)
    }
    

提交回复
热议问题