有没有办法知道 iOS 是否支持表情符号?

Is there a way to know if an Emoji is supported in iOS?

我正在构建一个 iOS 应用程序,表情符号在其中发挥了重要作用。

In iOS 10.2, new emojis were released.

我很确定,如果有人 iOS 8,例如,他们实际上看不到这些表情符号。有没有办法检测到这一点?我正在尝试动态构建用户 iOS 版本支持的所有表情符号的列表,但我遇到了一些麻烦。

澄清:Emoji 只是 Unicode 字符中的一个字符 space,因此目前的解决方案适用于所有字符,而不仅仅是 Emoji。

剧情简介

要知道 Unicode 字符(包括表情符号)是否在给定设备上可用或 OS、运行 下面的 unicodeAvailable() 方法。

它的工作原理是将给定字符 image 与已知的未定义 Unicode 字符 U+1FFF.

进行比较

unicodeAvailable(),一个 Character 扩展

private static let refUnicodeSize: CGFloat = 8
private static let refUnicodePng =
    Character("\u{1fff}").png(ofSize: Character.refUnicodeSize)

func unicodeAvailable() -> Bool {
    if let refUnicodePng = Character.refUnicodePng,
        let myPng = self.png(ofSize: Character.refUnicodeSize) {
        return refUnicodePng != myPng
    }
    return false
}

讨论

  1. 所有字符将呈现为 png,大小与

    中定义的相同 (8)

    static let refUnicodeSize: CGFloat = 8

  2. 未定义字符U+1FFF图像计算一次

    static let refUnicodePng = Character("\u{1fff}").png(ofSize: Character.refUnicodeSize)

  3. 辅助方法可选择从 Character

    创建 png

    func png(ofSize fontSize: CGFloat) -> Data?

1。示例:针对 3 个表情符号进行测试

let codes:[Character] = ["\u{2764}","\u{1f600}","\u{1F544}"] // ❤️, , undefined
for unicode in codes {
    print("\(unicode) : \(unicode.unicodeAvailable())")
}

2。示例:测试一系列 Unicode 字符

func unicodeRange(from: Int, to: Int) {
    for unicodeNumeric in from...to {
        if let scalar = UnicodeScalar(unicodeNumeric) {
            let unicode = Character(scalar)
            let avail = unicode.unicodeAvailable()
            let hex = String(format: "0x%x", unicodeNumeric)
            print("\(unicode) \(hex) is \(avail ? "" : "not ")available")
        }
    }
}


辅助函数:Characterpng

func png(ofSize fontSize: CGFloat) -> Data? {
    let attributes = [NSAttributedStringKey.font:
                          UIFont.systemFont(ofSize: fontSize)]
    let charStr = "\(self)" as NSString
    let size = charStr.size(withAttributes: attributes)

    UIGraphicsBeginImageContext(size)
    charStr.draw(at: CGPoint(x: 0,y :0), withAttributes: attributes)

    var png:Data? = nil
    if let charImage = UIGraphicsGetImageFromCurrentImageContext() {
        png = UIImagePNGRepresentation(charImage)
    }

    UIGraphicsEndImageContext()
    return png
}

► 在 GitHub and a detailed article on Swift Recipes 上找到此解决方案。

仅供将来参考,在发现我的应用程序有 12.x 版本不支持的 13.2 表情符号后,我使用了这里的答案:How can I determine if a specific emoji character can be rendered by an iOS device? 这对我来说非常有效。