Last active
December 26, 2015 02:19
-
-
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. ✨
This file contains 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
@interface NSString (KKEmojiHandling) | |
- (NSUInteger)KK_composedLength; | |
@end |
This file contains 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
@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