Skip to content

Instantly share code, notes, and snippets.

@krummler
Last active September 3, 2024 11:40
Show Gist options
  • Save krummler/879e1ce942893db3104783d1d0e67b34 to your computer and use it in GitHub Desktop.
Save krummler/879e1ce942893db3104783d1d0e67b34 to your computer and use it in GitHub Desktop.
Emoji Checking for Swift 5.0 and up
import Foundation
extension Character {
/// A simple emoji is one scalar and presented to the user as an Emoji
var isSimpleEmoji: Bool {
return unicodeScalars.count == 1 && unicodeScalars.first?.properties.isEmojiPresentation ?? false
}
/// Checks if the scalars will be merged into and emoji
var isCombinedIntoEmoji: Bool {
return unicodeScalars.count > 1 &&
unicodeScalars.contains { $0.properties.isJoinControl || $0.properties.isVariationSelector }
}
var isEmoji: Bool {
return isSimpleEmoji || isCombinedIntoEmoji
}
}
extension String {
var isSingleEmoji: Bool {
return count == 1 && containsEmoji
}
var containsEmoji: Bool {
return contains { $0.isEmoji }
}
var containsOnlyEmoji: Bool {
return !isEmpty && !contains { !$0.isEmoji }
}
var emojiString: String {
return emojis.map { String($0) }.reduce("", +)
}
var emojis: [Character] {
filter { $0.isEmoji }
}
var emojiScalars: [UnicodeScalar] {
return filter{ $0.isEmoji }.flatMap { $0.unicodeScalars }
}
}
"A̛͚̖".containsEmoji // false
"3".containsEmoji // false
"3️⃣".isSingleEmoji // true
"👌🏿".isSingleEmoji // true
"🙎🏼‍♂️".isSingleEmoji // true
"👨‍👩‍👧‍👧".isSingleEmoji // true
"👨‍👩‍👧‍👧".containsOnlyEmoji // true
"Hello 👨‍👩‍👧‍👧".containsOnlyEmoji // false
"Hello 👨‍👩‍👧‍👧".containsEmoji // true
"👫 Héllo 👨‍👩‍👧‍👧".emojiString // "👫👨‍👩‍👧‍👧"
"👨‍👩‍👧‍👧".count // 1
"👫 Héllœ 👨‍👩‍👧‍👧".emojiScalars // [128107, 128104, 8205, 128105, 8205, 128103, 8205, 128103]
"👫 Héllœ 👨‍👩‍👧‍👧".emojis // ["👫", "👨‍👩‍👧‍👧"]
"👫 Héllœ 👨‍👩‍👧‍👧".emojis.count // 2
"👫👨‍👩‍👧‍👧👨‍👨‍👦".isSingleEmoji // false
"👫👨‍👩‍👧‍👧👨‍👨‍👦".containsOnlyEmoji // true
@atultw
Copy link

atultw commented Jan 17, 2023

In line 12 make sure to add isEmojiModifier to the OR, otherwise skin tone emojis won't be recognized

@MohsenKhosravinia
Copy link

It doesn't recognize these characters as emojis:
‼, ⁉, ©, ®, ↔, ↕, ↖, ↗, ↘, ↙, ↩, ↪, ⌚, ⌛, ⌨, ℹ, ™

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment