判断Emoji在当前系统版本能否正常显示

    /// 当前系统是否支持emoji展示,以下两个判断条件
    /// 1.必须字体库支持 2.展示时不能被分割成多个emoji
    private var isEmojiSupported: Bool {
        // 字符数不应该超过1
        guard count == 1 else {
            return false
        }
        let uniChars = Array(utf16)
        let font = CTFontCreateWithName("AppleColorEmoji" as CFString, 0.0, nil)
        var glyphs: [CGGlyph] = [0, 0]
        let visible = CTFontGetGlyphsForCharacters(font, uniChars, &glyphs, uniChars.count)
        let emojiWidth = ceil(self.size(withAttributes: [.font: UIFont.systemFont(ofSize: 12)]).width)
        let standardEmojiWidth = ceil("".size(withAttributes: [.font: UIFont.systemFont(ofSize: 12)]).width)
        let unseparated = emojiWidth == standardEmojiWidth
        return visible && unseparated
    }

你可能感兴趣的:(iOS开发,swift,ios)