iOS 判定字符串是否包含emoji

作者: 跳跳跳跳跳跳跳 | 来源:发表于2020-06-08 10:26 被阅读0次

    项目有需求需要判定字符串中是否包含表情包,于是百度,google啥手段都用尽了,试了一个又一个的方法,最后发现下面这段代码靠谱点

    - (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;
                                                NSLog(@"%@",substring);
                                                [self changeToString:substring];
                                            }
                                        }
                                    } 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;
    }
    

    然后美滋滋的去实验,结果发现很多新出的emoji都不能识别出来,估摸着是emoji判定的范围的不对。就想着更新最新的emoji的unicode码范围。
    用control+command+空格弹出字符检视器,就能看到系统自带的emoji,但是现在还看不到emoji的unicode码。点击左上角的设置图标


    截屏2020-06-04 下午4.50.23.png
    选择自定列表,然后一直往下拉,勾选代码表里的Unicode 截屏2020-06-04 下午4.51.56.png
    完成后,返回再点击随意一个emoji表情,在右侧就有该emoji的unicode码了。 截屏2020-06-04 下午4.53.04.png
    再然后就是茫茫多的去比对unicode值了。最后更新最后的结果代码
    - (BOOL)isContainEmoji {
        __block BOOL containsEmoji = NO;
        [self enumerateSubstringsInRange:NSMakeRange(0,[self length])
                                 options:NSStringEnumerationByComposedCharacterSequences
                              usingBlock:^(NSString *substring,
                                           NSRange substringRange,
                                           NSRange enclosingRange,
                                           BOOL *stop) {
             const unichar hs = [substring characterAtIndex:0];
             // surrogate pair
             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 <= 0x1fa95) {
                         containsEmoji = YES;
                     }
                 }
             }else if (substring.length > 1) {
                 const unichar ls = [substring characterAtIndex:1];
                 if (ls == 0x20e3 ||
                     ls == 0xfe0f ||
                     ls == 0xd83c) {
                     containsEmoji = YES;
                 }
             } else {
                 // non surrogate
                 if (0x2100 <= hs && hs <= 0x27ff) {
                     containsEmoji = YES;
                 }else if (0x2B05 <= hs && hs <= 0x2b07) {
                     containsEmoji = YES;
                 } else if (0x2934 <= hs && hs <= 0x2935) {
                     containsEmoji = YES;
                 }else if (0x3297 <= hs && hs <= 0x3299) {
                     containsEmoji = YES;
                 }else if (hs == 0xa9 ||
                          hs == 0xae ||
                          hs == 0x303d ||
                          hs == 0x3030 ||
                          hs == 0x2b55 ||
                          hs == 0x2b1c ||
                          hs == 0x2b1b ||
                          hs == 0x2b50) {
                     containsEmoji = YES;
                 }
             }
             if (containsEmoji) {
                 *stop = YES;
             }
         }];
        return containsEmoji;
    }
    

    验证于IphoneXS iOS 13.2.3版本
    如有问题,请提出指教

    相关文章

      网友评论

        本文标题:iOS 判定字符串是否包含emoji

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