to check characters limit after change func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool { let currentText = textField.text ?? "" let newText = (currentText as NSString) .replacingCharacters(in: range, with: string) return newText.count <= 3 }