标签:
在做项目的时候,textField能够输入表情,但是iOS的表情是占两个字符的,再计算字符串长度的时候就和想象的不一样了,所以用了次方法会将表情的长度转成1,最后得到的字符串的长度就是能看到的实际的长度。
方法中用到了iOS的表情编码,看不懂具体是什么,但是知道那些就是表情的编码,根据这个编码就能够找到具体是那个表情了。
此方法参考自:http://blog.csdn.net/liujinlongxa/article/details/44207003
- (NSInteger)getStringLengthWithString:(NSString *)string
{
__block NSInteger stringLength = 0;
[string enumerateSubstringsInRange:NSMakeRange(0, [string length])
options:NSStringEnumerationByComposedCharacterSequences
usingBlock:^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop)
{
const unichar hs = [substring characterAtIndex:0];
if (0xd800 <= hs && hs <= 0xdbff)
{
if (substring.length > 1)
{
const unichar ls = [substring characterAtIndex:1];
const int uc = ((hs - 0xd800) * 0x400) + (ls - 0xdc00) + 0x10000;
if (0x1d000 <= uc && uc <= 0x1f77f)
{
stringLength += 1;
}
else
{
stringLength += 1;
}
}
else
{
stringLength += 1;
}
} else if (substring.length > 1)
{
const unichar ls = [substring characterAtIndex:1];
if (ls == 0x20e3)
{
stringLength += 1;
}
else
{
stringLength += 1;
}
} else {
if (0x2100 <= hs && hs <= 0x27ff)
{
stringLength += 1;
}
else if (0x2B05 <= hs && hs <= 0x2b07)
{
stringLength += 1;
}
else if (0x2934 <= hs && hs <= 0x2935)
{
stringLength += 1;
}
else if (0x3297 <= hs && hs <= 0x3299)
{
stringLength += 1;
}
else if (hs == 0xa9 || hs == 0xae || hs == 0x303d || hs == 0x3030 || hs == 0x2b55 || hs == 0x2b1c || hs == 0x2b1b || hs == 0x2b50)
{
stringLength += 1;
}
else
{
stringLength += 1;
}
}
}];
return stringLength;
}
附文:
iOS表情编码集:
标签:
原文地址:http://www.cnblogs.com/xiaowei4554/p/4611345.html