检测用户是否在UITextView中键入了表情符号字符

Alb*_*haw 13 unicode objective-c detect ios emoji

我有一个UITextView,我需要检测用户是否输入表情符号字符.

我认为只检查最新字符的unicode值就足够了,但是对于新的表情符号2s,一些字符分散在整个unicode索引中(即Apple新设计的版权和注册徽标).

也许与使用NSLocale或LocalizedString值检查字符的语言有关?

有谁知道一个好的解决方案?

谢谢!

Alb*_*haw 18

多年来,这些表情符号检测解决方案一直在破坏,因为Apple添加了新的表情符号(如通过预先诅咒带有附加角色的角色建立的肤色表情符号)等等.

我终于崩溃了,只是写了下面的方法,适用于所有当前的表情符号,应该适用于所有未来的表情符号.

该解决方案创建了一个带有角色和黑色背景的UILabel.然后,CG拍摄标签的快照,并扫描快照中的所有像素,以查找任何非纯黑像素.我添加黑色背景的原因是为了避免因子像素渲染而导致的错误着色问题

解决方案在我的设备上运行非常快,我可以每秒检查数百个字符,但应该注意这是一个CoreGraphics解决方案,不应该像使用常规文本方法那样大量使用.图形处理数据量很大,因此一次检查数千个字符可能会导致明显的延迟.

-(BOOL)isEmoji:(NSString *)character {

    UILabel *characterRender = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 1, 1)];
    characterRender.text = character;
    characterRender.backgroundColor = [UIColor blackColor];//needed to remove subpixel rendering colors
    [characterRender sizeToFit];

    CGRect rect = [characterRender bounds];
    UIGraphicsBeginImageContextWithOptions(rect.size,YES,0.0f);
    CGContextRef contextSnap = UIGraphicsGetCurrentContext();
    [characterRender.layer renderInContext:contextSnap];
    UIImage *capturedImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    CGImageRef imageRef = [capturedImage CGImage];
    NSUInteger width = CGImageGetWidth(imageRef);
    NSUInteger height = CGImageGetHeight(imageRef);
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    unsigned char *rawData = (unsigned char*) calloc(height * width * 4, sizeof(unsigned char));
    NSUInteger bytesPerPixel = 4;
    NSUInteger bytesPerRow = bytesPerPixel * width;
    NSUInteger bitsPerComponent = 8;
    CGContextRef context = CGBitmapContextCreate(rawData, width, height,
                                                 bitsPerComponent, bytesPerRow, colorSpace,
                                                 kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
    CGColorSpaceRelease(colorSpace);

    CGContextDrawImage(context, CGRectMake(0, 0, width, height), imageRef);
    CGContextRelease(context);

    BOOL colorPixelFound = NO;

    int x = 0;
    int y = 0;
    while (y < height && !colorPixelFound) {
        while (x < width && !colorPixelFound) {

            NSUInteger byteIndex = (bytesPerRow * y) + x * bytesPerPixel;

            CGFloat red = (CGFloat)rawData[byteIndex];
            CGFloat green = (CGFloat)rawData[byteIndex+1];
            CGFloat blue = (CGFloat)rawData[byteIndex+2];

            CGFloat h, s, b, a;
            UIColor *c = [UIColor colorWithRed:red green:green blue:blue alpha:1.0f];
            [c getHue:&h saturation:&s brightness:&b alpha:&a];

            b /= 255.0f;

            if (b > 0) {
                colorPixelFound = YES;
            }

            x++;
        }
        x=0;
        y++;
    }

    return colorPixelFound;

}
Run Code Online (Sandbox Code Playgroud)

  • @gschandler是的,除非他们复制粘贴表情符号,或者使用具有表情符号字符但将主要语言设置为英语的自定义键盘(iOS8应用程序扩展)。 (2认同)
  • 编辑掉我的旧解决方案并编写CG解决方案,忽略以前的评论 (2认同)