美文网首页
2019 iOS最新最全检测Emoji方法(OC版、Swift版

2019 iOS最新最全检测Emoji方法(OC版、Swift版

作者: Yo_Moon | 来源:发表于2019-04-25 13:42 被阅读0次

    最近项目需要整emoji,然后在网上一搜,全是无脑copy党...可恨的还copy很老的代码根本不适应。虽然有的适应,但是检测功能不全,还得经常更新,甚是麻烦...

    ps:如果是因为服务器不支持Emoji,需要剔除Emoji的话,其实不用那么麻烦,把字符串内容Unicode编码一下就OK了,另附一个Unicode编码/解码

    2019.7.1更新加上了matchesInStringWithRegex方法。

    2020.9.10 增加Swift版

    不废话 上代码,让广大码农来检测。。。

    
    =======================================================
    
    OC版:
    
    - (NSArray *)isContainsEmoji {
        
        NSMutableArray *array1 = [[NSMutableArray alloc] init];
        NSMutableArray *array2 = [[NSMutableArray alloc] init];
        
        NSRange range;
        NSString *emoji = @"aas d!`2完  全䴎无力😀😃😄😁😆🤩🥳😏😒😞👌🏿🤘🏻👆🏾👩‍❤️‍💋‍👩👩‍👩‍👦‍👦👩‍👩‍👧🌴🌳⚽️🇧🇬🇧🇧🔯📞🚨🏸。。..~#$, 包括生僻字(𤋮)";
        
        for (int i = 0; i < emoji.length; i += range.length) {
            
            range = [emoji rangeOfComposedCharacterSequenceAtIndex:i];
            NSString *string = [emoji substringWithRange:range];
            NSUInteger length = [string lengthOfBytesUsingEncoding:NSUTF8StringEncoding];
            
            if (length >= 4) {
                
                //正则4个字节或以上的生僻字、其他字符集
                NSString *regex = @"[\\uD840\\uDC00-\\uD87A\\uDFEF]";
                NSArray *matches = [string matchesInStringWithRegex:regex];
                
                if (!matches.count) {
                    [array1 addObject:MOStringWithFormat(@"字符串:'%@', 字节:'%ld'", string, length)];
                } else {
                    [array2 addObject:MOStringWithFormat(@"字符串:'%@', 字节:'%ld'", string, length)];
                }
            } else {
                [array2 addObject:MOStringWithFormat(@"字符串:'%@', 字节:'%ld'", string, length)];
            }
        }
        
        NSLog(@"Emoji>> %@, \n非Emoji>> %@", array1, array2);
        
        return array1;
    }
    
    - (NSArray<NSTextCheckingResult *> *)matchesInStringWithRegex:(NSString *)regex {
        
        NSString *string = [NSString stringWithString:self];
        
        NSRegularExpression *regular = [NSRegularExpression regularExpressionWithPattern:regex options:NSRegularExpressionCaseInsensitive error:nil];
        
        NSArray *matches = [regular matchesInString:string options:NSMatchingReportCompletion range:NSMakeRange(0, string.length)];
        
        if (!matches.count) {
            return nil;
        }
        
        return matches;
    }
    
    =======================================================
    
    log:
    
    Emoji>> (
        "字符串:'😀', 字节:'4'",
        "字符串:'😃', 字节:'4'",
        "字符串:'😄', 字节:'4'",
        "字符串:'😁', 字节:'4'",
        "字符串:'😆', 字节:'4'",
        "字符串:'🤩', 字节:'4'",
        "字符串:'🥳', 字节:'4'",
        "字符串:'😏', 字节:'4'",
        "字符串:'😒', 字节:'4'",
        "字符串:'😞', 字节:'4'",
        "字符串:'👌🏿', 字节:'8'",
        "字符串:'🤘🏻', 字节:'8'",
        "字符串:'👆🏾', 字节:'8'",
        "字符串:'👩‍❤️‍💋‍👩', 字节:'27'",
        "字符串:'👩‍👩‍👦‍👦', 字节:'25'",
        "字符串:'👩‍👩‍👧', 字节:'18'",
        "字符串:'🌴', 字节:'4'",
        "字符串:'🌳', 字节:'4'",
        "字符串:'⚽️', 字节:'6'",
        "字符串:'🇧🇬', 字节:'8'",
        "字符串:'🇧🇧', 字节:'8'",
        "字符串:'🔯', 字节:'4'",
        "字符串:'📞', 字节:'4'",
        "字符串:'🚨', 字节:'4'",
        "字符串:'🏸', 字节:'4'",
    ), 
    非Emoji>> (
        "字符串:'a', 字节:'1'",
        "字符串:'a', 字节:'1'",
        "字符串:'s', 字节:'1'",
        "字符串:' ', 字节:'1'",
        "字符串:'d', 字节:'1'",
        "字符串:'!', 字节:'1'",
        "字符串:'`', 字节:'1'",
        "字符串:'2', 字节:'1'",
        "字符串:'完', 字节:'3'",
        "字符串:' ', 字节:'1'",
        "字符串:' ', 字节:'1'",
        "字符串:'全', 字节:'3'",
        "字符串:'䴎', 字节:'3'",
        "字符串:'无', 字节:'3'",
        "字符串:'力', 字节:'3'",
        "字符串:'。', 字节:'3'",
        "字符串:'。', 字节:'3'",
        "字符串:'.', 字节:'1'",
        "字符串:'.', 字节:'1'",
        "字符串:'~', 字节:'1'",
        "字符串:'#', 字节:'1'",
        "字符串:'$', 字节:'1'",
        "字符串:',', 字节:'1'",
        "字符串:' ', 字节:'1'",
        "字符串:'包', 字节:'3'",
        "字符串:'括', 字节:'3'",
        "字符串:'生', 字节:'3'",
        "字符串:'僻', 字节:'3'",
        "字符串:'字', 字节:'3'",
        "字符串:'(', 字节:'1'",
        "字符串:'𤋮', 字节:'4'",
        "字符串:')', 字节:'1'",
    )
    =======================================================
    
    Swift版:
    
        /// 检测是否包含Emoji表情
        var isContainsEmoji: Bool {
            for char in self {
                let str = String(char)
                if str.lengthOfBytes(using: .utf8) >= 4 {
                    if str.matches("[\\uD840\\uDC00-\\uD87A\\uDFEF]").isEmpty {
                        return true
                    }
                }
            }
            return false
        }
    
        /// 匹配正则表达式,并返回匹配结果
        /// - Parameter regex: 正则表达式
        /// - Returns: 匹配结果
        func matches(_ regex: String) -> [NSTextCheckingResult] {
            guard let regular = try? NSRegularExpression(pattern: regex, options: .caseInsensitive) else {
                return []
            }
            return regular.matches(in: self, options: .reportCompletion, range: NSRange(location: 0, length: count))
        }
    
    =======================================================
    
    

    该方法无需更新所谓的列表,可以无视后续增加emoji,一劳永逸。

    妈妈再也不用担心检测不到emoji了!!!

    欢迎吐槽!

    相关文章

      网友评论

          本文标题:2019 iOS最新最全检测Emoji方法(OC版、Swift版

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