Skip to content

Instantly share code, notes, and snippets.

@kristopherjohnson
Created October 8, 2012 02:21
Show Gist options
  • Save kristopherjohnson/3850397 to your computer and use it in GitHub Desktop.
Save kristopherjohnson/3850397 to your computer and use it in GitHub Desktop.
Determine whether proposed change to UITextField will exceed maximum text length
-(BOOL)shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string textField:(UITextField *)textField maxLength:(NSUInteger)maxTextFieldLength
{
NSUInteger oldLength = [textField.text length];
NSUInteger replacementLength = [string length];
NSUInteger rangeLength = range.length;
NSUInteger newLength = oldLength - rangeLength + replacementLength;
return newLength <= maxTextFieldLength;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment