美文网首页IOSiOS进阶指南
iOS 关于表情符号的一些问题

iOS 关于表情符号的一些问题

作者: zhanghengiOS | 来源:发表于2016-08-22 17:04 被阅读960次

    z在项目中,要求用户评论超过10行或者超过400字节显示“...read more”类似微信的展开全文,这个功能已经做好,突然QA告诉我说,有条数据没显示,但是进入用户评论的详情页,数据是有的。查了一下接口,service返回的数据没有问题。

    经过仔细观察发现地四百个字符正好是表情符号,查了一些资料发现iOS中表情符号占位是两位,这样就正好截取了表情符号的一半,所以不会显示,用了两个方法来解决这个问题,一个是获取到字符串的真正长度

    - (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;

    }

    还有一个方法是判断字符串中是否有表情符号

    + (BOOL)stringContainsEmoji:(NSString *)string

    {

    __block BOOL returnValue = NO;

    [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) {

    returnValue = YES;

    }

    }

    } else if (substring.length > 1) {

    const unichar ls = [substring characterAtIndex:1];

    if (ls == 0x20e3) {

    returnValue = YES;

    }

    } else {

    if (0x2100 <= hs && hs <= 0x27ff) {

    returnValue = YES;

    } else if (0x2B05 <= hs && hs <= 0x2b07) {

    returnValue = YES;

    } else if (0x2934 <= hs && hs <= 0x2935) {

    returnValue = YES;

    } else if (0x3297 <= hs && hs <= 0x3299) {

    returnValue = YES;

    } else if (hs == 0xa9 || hs == 0xae || hs == 0x303d || hs == 0x3030 || hs == 0x2b55 || hs == 0x2b1c || hs == 0x2b1b || hs == 0x2b50) {

    returnValue = YES;

    }

    }

    }];

    return returnValue;

    }

    进行判断的核心代码如下

    if(!readMore && [self getStringLengthWithString:summarize]>300){

    if ([HomeReviewTableViewCell stringContainsEmoji:[summarize substringWithRange:NSMakeRange(299,2)]]){

    summarize=[NSString stringWithFormat:@"%@%@",[summarize substringToIndex:299] ,LINK_TEXT];

    }else if ([HomeReviewTableViewCell stringContainsEmoji:[summarize substringWithRange:NSMakeRange(300,2)]]) {

    summarize=[NSString stringWithFormat:@"%@%@",[summarize substringToIndex:300] ,LINK_TEXT];

    }else{

    summarize=[NSString stringWithFormat:@"%@%@",[summarize substringToIndex:300],LINK_TEXT];

    }

    相关文章

      网友评论

        本文标题:iOS 关于表情符号的一些问题

        本文链接:https://www.haomeiwen.com/subject/oypihttx.html