带标志表情符号的NSString长度 [英] NSString length with flag emojis
问题描述
我正在尝试获取NSString的长度,该长度包含一堆表情符号,包括标志字符。现在,我知道如何获取包含表情符号的字符串的长度:
I'm trying to get the length of an NSString that contains a bunch of emojis, including the flag characters. Now I know how to get the length of a string containing emojis:
__block NSInteger length = 0;
[string enumerateSubstringsInRange:range
options:NSStringEnumerationByComposedCharacterSequences
usingBlock:^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop) {
length++;
}];
但是, NSStringEnumerationByComposedCharacterSequences在这种情况下不起作用。普通的表情符号由两个字符组成。标志表情符号由两个类似表情符号的字符组成。因此,一个没有任何枚举技术的标志表情符号的长度为4。
However, "NSStringEnumerationByComposedCharacterSequences" doesn't work in this case. Normal emojis are composed of two characters put together. The flag emojis are composed of two emoji-like characters put together. Thus the length of one flag emoji without any enumeration technique is 4.
如何获取包含标志表情符号的NSString的正确长度(字符数)?
How do I get the proper length (number of characters) of an NSString containing flag emojis?
推荐答案
我尝试了以下解决方案。
对于普通表情符号,它返回2,对于标志,则返回4。
这也适用于普通字符和特殊字符。
I tried the below solution. For normal emoji it returns 2 and for flags it will give 4. Also this will work with normal chars and special chars.
const char *cString = [string UTF8String];
int textLength = (int)strlen(cString);
这篇关于带标志表情符号的NSString长度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!