Skip to content

Instantly share code, notes, and snippets.

@DisappearPing
Created March 24, 2022 08:05
Show Gist options
  • Save DisappearPing/b677eb59a1ac98e92f0cc90884963b37 to your computer and use it in GitHub Desktop.
Save DisappearPing/b677eb59a1ac98e92f0cc90884963b37 to your computer and use it in GitHub Desktop.
TextField limit enter string length
// TextField
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
guard let textFieldText = textField.text,
let rangeOfTextToReplace = Range(range, in: textFieldText) else {
return false
}
let substringToReplace = textFieldText[rangeOfTextToReplace]
let count = textFieldText.count - substringToReplace.count + string.count
return count <= 10
}
@DisappearPing
Copy link
Author

limit for 10 digit enter for example usage

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment