iOS 包含表情判断 Emoji in Swift New

找比较全面的表情判断看这里就行了 NEW

extension Character {
    var isSimpleEmoji: Bool {
        guard let firstScalar = unicodeScalars.first else {
            return false
        }
        return firstScalar.properties.isEmoji && firstScalar.value > 0x238C
    }
    var isCombinedIntoEmoji: Bool {
        unicodeScalars.count > 1 && unicodeScalars.first?.properties.isEmoji ?? false
    }
    var isEmoji: Bool { isSimpleEmoji || isCombinedIntoEmoji }
}

extension String {
    var isSingleEmoji: Bool {
        return count == 1 && containsEmoji
    }
    var containsEmoji: Bool {//是否包含emoji
        return contains { $0.isEmoji }
    }
    var containsOnlyEmoji: Bool {//是否全是emoji
        return !isEmpty && !contains { !$0.isEmoji }
    }
    var emojiString: String {
        return emojis.map { String($0) }.reduce("", +)
    }
    var emojis: [Character] {
        return filter { $0.isEmoji }
    }
    var emojiScalars: [UnicodeScalar] {
        return filter { $0.isEmoji }.flatMap { $0.unicodeScalars }
    }
    func stringByRemovingEmoji() -> String {//删除字符串中的emoji
        return String(self.filter { !($0.isEmoji) })
    }
}

textField 禁止输入表情

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
    if let text = textField.text,
       let textRange = Range(range, in: text) {
       let updatedText = text.replacingCharacters(in: textRange, with: string)
        if updatedText.containsEmoji {
            return false
        }
    }
    return true
}

简单、实用
参考

你可能感兴趣的:(iOS 包含表情判断 Emoji in Swift New)