Skip to content

Instantly share code, notes, and snippets.

@mlaster
Created October 15, 2012 22:39
Show Gist options
  • Save mlaster/3896122 to your computer and use it in GitHub Desktop.
Save mlaster/3896122 to your computer and use it in GitHub Desktop.
Korean-aware character length limiting
- (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