Skip to content

Instantly share code, notes, and snippets.

@truongngoclinh
Created July 12, 2017 10:41
Show Gist options
  • Save truongngoclinh/3f53f8c5253d6a2c65a0a79379e7baaf to your computer and use it in GitHub Desktop.
Save truongngoclinh/3f53f8c5253d6a2c65a0a79379e7baaf to your computer and use it in GitHub Desktop.
Set maxLength = 2 to UITextField
- (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;
}
@truongngoclinh
Copy link
Author

set delegate to UITextFieldDelegate

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