Set the maximum character length of a UITextField

前端 未结 30 2081
难免孤独
难免孤独 2020-11-22 02:27

How can I set the maximum amount of characters in a UITextField on the iPhone SDK when I load up a UIView?

30条回答
  •  一整个雨季
    2020-11-22 03:05

    What about this simple approach. Its working fine for me.

    extension UITextField {
    
        func  charactersLimit(to:Int) {
    
            if (self.text!.count > to) {
                self.deleteBackward()
            }
        }
    }
    

    Then:

    someTextField.charactersLimit(to:16)
    

提交回复
热议问题