Max length UITextField

后端 未结 18 1825
一个人的身影
一个人的身影 2020-11-30 17:42

When I\'ve tried How to you set the maximum number of characters that can be entered into a UITextField using swift?, I saw that if I use all 10 characters, I can\'t erase t

18条回答
  •  渐次进展
    2020-11-30 18:32

    In Swift 4

    10 Characters limit for text field and allow to delete(backspace)

    func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
            if textField ==  userNameFTF{
                let char = string.cString(using: String.Encoding.utf8)
                let isBackSpace = strcmp(char, "\\b")
                if isBackSpace == -92 {
                    return true
                }
                return textField.text!.count <= 9
            }
            return true
        }
    

提交回复
热议问题