Created
July 12, 2017 10:41
-
-
Save truongngoclinh/3f53f8c5253d6a2c65a0a79379e7baaf to your computer and use it in GitHub Desktop.
Set maxLength = 2 to UITextField
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 | |
{ | |
// Prevent crashing undo bug | |
if(range.length + range.location > textField.text.length) | |
{ | |
return NO; | |
} | |
NSUInteger newLength = [textField.text length] + [string length] - range.length; | |
return newLength <= 2; | |
} |
Author
truongngoclinh
commented
Jul 12, 2017
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment