Skip to content

Instantly share code, notes, and snippets.

@bmitchelmore
Created March 19, 2013 14:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bmitchelmore/5196446 to your computer and use it in GitHub Desktop.
Save bmitchelmore/5196446 to your computer and use it in GitHub Desktop.
Implementing maxlength for UITextField. This will prevent any character sequence from being inserted that would make the textfield's text exceed the maxlength. If you paste in multicharacter sequences that will make the field exceed its max length, the entire sequence will be rejected.
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
NSInteger result = textField.text.length;
result -= range.length;
result += string.length;
return result <= MAX_LENGTH;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment