Skip to content

Instantly share code, notes, and snippets.

@darhonbek
Created February 17, 2018 10:25
Show Gist options
  • Save darhonbek/a2f769e9a41c3b0827baa55143532e9a to your computer and use it in GitHub Desktop.
Save darhonbek/a2f769e9a41c3b0827baa55143532e9a to your computer and use it in GitHub Desktop.
Ensure # of chars dose not exceed the limit (UITextField)
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
let nsString = textField.text as NSString?
let newString = nsString?.replacingCharacters(in: range, with: string)
if let string = newString,
string.count > YOUR_LIMIT_HERE {
return false
}
return true
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment