Uppercase characters in UItextfield

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

    Swift 4.0 Version:

    First set the delegate for the textfield you want to uppercase to the current ViewController (click drag from the textfield to the currentViewController to set the delegate).

    After add the extension:

    extension CurrentViewController: UITextFieldDelegate{
    
    func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
    
            //refference to the textfield you want to target
            if textField.tag == 5{
                textField.text = (textField.text! as NSString).replacingCharacters(in: range, with: string.uppercased())
                return false
            }
            return true
        }
    }
    

提交回复
热议问题