Created
March 19, 2013 14:15
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (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