Swift中统计emoji个数

extension String {
func countEmojiCharacter() -> Int {

    func isEmoji(s:NSString) -> Bool {

        let high:Int = Int(s.characterAtIndex(0))
        if 0xD800 <= high && high <= 0xDBFF {
            let low:Int = Int(s.characterAtIndex(1))
            let codepoint: Int = ((high - 0xD800) * 0x400) + (low - 0xDC00) + 0x10000
            return (0x1D000 <= codepoint && codepoint <= 0x1F9FF)
        }
        else {
            return (0x2100 <= high && high <= 0x27BF)
        }
    }

    let nsString = self as NSString
    var length = 0

    nsString.enumerateSubstringsInRange(NSMakeRange(0, nsString.length), options: NSStringEnumerationOptions.ByComposedCharacterSequences) { (subString, substringRange, enclosingRange, stop) -> Void in

        if isEmoji(subString!) {
            length++
        }
    }

    return length
}

}

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/997056.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-21
下一篇 2022-05-21

发表评论

登录后才能评论

评论列表(0条)

保存