0

How can I control the max length of each textfield? For example, I want set max len for txtname = 10. So while typing, if the count of characters is more than 10, it should stop typing. I use the below code but it doesn't work:

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
    guard let text = textField.text else { return true }
    let newLength = text.characters.count + string.characters.count - range.length
    return newLength <= 10 // Bool
}
Vega
  • 27,856
  • 27
  • 95
  • 103
ali
  • 43
  • 6

0 Answers0