I am making an app where I have multiple text fields and I need to set a maximum character length for each. Note: Each textfield will have a different maximum length.
So far I have tried:
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
let maxLength = 30
let currentString: NSString = brandTextField.text! as NSString
let newString: NSString =
currentString.replacingCharacters(in: range, with: string) as NSString
return newString.length <= maxLength
}
But this only works for one text field and I can't specify different lengths for different text fields.
Thanks for any help with this matter!