美文网首页
iOS 表情筛选 emoji

iOS 表情筛选 emoji

作者: 搬砖小工 | 来源:发表于2017-04-11 09:39 被阅读147次

很多时候我们需要判断用户输入的文字是否是表情,因为有些地方不允许用户输入表情,例如名字,昵称,个性签名等。网上也有很多方法检测,但大部分都是有漏洞的。所以在这里贴上一段代码,亲测有用测试输入的表情都能判断出来,如果大家用的有问题,敬请相告,改之。谢谢!

- (BOOL)isEmoji:(NSString *)string
{
    if ([string length]<2)
    {
        return NO;
    }

    static NSCharacterSet *_variationSelectors;
    _variationSelectors = [NSCharacterSet characterSetWithRange:NSMakeRange(0xFE00, 16)];

    if ([string rangeOfCharacterFromSet: _variationSelectors].location != NSNotFound) 
    {
        return YES;
    }
    const unichar high = [string characterAtIndex:0];
    // Surrogate pair (U+1D000-1F9FF)
    if (0xD800 <= high && high <= 0xDBFF) 
    {
        const unichar low = [string characterAtIndex: 1];
        const int codepoint = ((high - 0xD800) * 0x400) + (low - 0xDC00) + 0x10000;
        return (0x1D000 <= codepoint && codepoint <= 0x1F9FF);
        // Not surrogate pair (U+2100-27BF)
    } 
    else 
    {
        return (0x2100 <= high && high <= 0x27BF);
    }
}
转载请注明出处,谢谢!

相关文章

网友评论

      本文标题:iOS 表情筛选 emoji

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