How can I determine if a specific emoji character can be rendered by an iOS device?

喜你入骨 提交于 2019-12-04 19:10:48
// Determine if the emoji character provided in the string can be rendered on this OS version
+ (BOOL)isEmojiSupported:(NSString *)emoji
{
    NSData *data = [string dataUsingEncoding:NSUTF32LittleEndianStringEncoding];
    UTF32Char emojiValue;
    [data getBytes:&emojiValue length:sizeof(emojiValue)];

    // Convert UTF32Char to UniChar surrogate pair.
    // Found here: http://stackoverflow.com/questions/13005091/how-to-tell-if-a-particular-font-has-a-specific-glyph-64k#
    UniChar characters[2] = { };
    CFIndex length = (CFStringGetSurrogatePairForLongCharacter(emojiValue, characters) ? 2 : 1);

    CGGlyph glyphs[2] = { };
    CTFontRef ctFont = CTFontCreateWithName((CFStringRef)@"AppleColorEmoji", 0.0, NULL);

    // If we don't get back any glyphs for the characters array, it's not supported
    BOOL ret = CTFontGetGlyphsForCharacters(ctFont, characters, glyphs, length);
    CFRelease(ctFont);
    return ret;

}

In response to the Swift question above, here's a Swift version based largely on an answer in this question: Get surrogate pairs from an emoji

func isEmojiSupported(emoji: String) -> Bool {
  let uniChars = Array(emoji.utf16)
  let font = CTFontCreateWithName("AppleColorEmoji", 0.0, nil)
  var glyphs: [CGGlyph] = [0, 0]
  return CTFontGetGlyphsForCharacters(font, uniChars, &glyphs, uniChars.count)
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!