Created
October 15, 2012 22:39
-
-
Save mlaster/3896122 to your computer and use it in GitHub Desktop.
Korean-aware character length limiting
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
- (void)textViewDidChange:(UITextView *)inTextView { | |
NSUInteger count = [[self text] length]; | |
__weak __typeof__(self) blockSelf = self; | |
if (count > self.characterLimit) { | |
NSMutableString *newString = [NSMutableString string]; | |
__block NSUInteger blockCount = 0; | |
[inTextView.text enumerateSubstringsInRange:NSMakeRange(0, [inTextView.text length]) | |
options:NSStringEnumerationByComposedCharacterSequences | |
usingBlock:^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop){ | |
[newString appendString:substring]; | |
blockCount++; | |
if (blockCount == blockSelf.characterLimit) { | |
*stop = YES; | |
} | |
}]; | |
inTextView.text = newString; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment