Skip to content

Instantly share code, notes, and snippets.

@kolinkrewinkel
Last active December 26, 2015 02:19
Show Gist options
  • Save kolinkrewinkel/7077307 to your computer and use it in GitHub Desktop.
Save kolinkrewinkel/7077307 to your computer and use it in GitHub Desktop.
NSString category for finding the composed-character length. Practical use for most locales is finding the display-length of an emoji-containing string. ✨
@interface NSString (KKEmojiHandling)
- (NSUInteger)KK_composedLength;
@end
@implementation NSString (KKEmojiHandling)
- (NSUInteger)KK_composedLength
{
__block NSUInteger length = 0;
[self enumerateSubstringsInRange:NSMakeRange(0, self.length) options:NSStringEnumerationByComposedCharacterSequences usingBlock:^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop) {
length++;
}];
return length;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment