Last active
August 20, 2016 05:46
-
-
Save HarrisHan/65d6a93fb9838e582c202e133305f8ec to your computer and use it in GitHub Desktop.
limit textField textView input char length
This file contains hidden or 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
+ (NSUInteger)countLengthOfString:(NSString *)textString { | |
NSUInteger strlength = 0; | |
char *p = (char *)[textString cStringUsingEncoding:NSUnicodeStringEncoding]; | |
for (int i = 0; i < [textString lengthOfBytesUsingEncoding:NSUnicodeStringEncoding]; i++) { | |
if (*p) { | |
p++; | |
strlength++; | |
} else { | |
p++; | |
} | |
} | |
return strlength; | |
} |
This file contains hidden or 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
// limit textField's char length | |
UITextRange *rangeOfMarked = [textField markedTextRange]; | |
if (!rangeOfMarked) { | |
if ([NSString countLengthOfString:textField.text] > LIMIT_CHARACTOR) { | |
NSString* tmp; | |
for (NSInteger i = textField.text.length; i > 0; i--) { | |
tmp = [textField.text substringToIndex:i]; | |
if ([NSString countLengthOfString:tmp] <= LIMIT_CHARACTOR) { | |
textField.text = tmp; | |
break; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment