Skip to content

Instantly share code, notes, and snippets.

@krummler
Last active December 26, 2023 10:21
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • 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
@james-william-r
Copy link

Thanks for sharing! This is a great approach! 🙏☺️

@lk251
Copy link

lk251 commented Aug 18, 2022

@krummler hi! thanks for sharing, sadly flags are not detected by this code. what ranges do flags have?

@YesCoach
Copy link

@lk251
I found flags's isEmojiPresentation is true, but it's scalar is not only one, it's plural(mostly 2, someone is 7(🏴󠁧󠁢󠁥󠁮󠁧󠁿,🏴󠁧󠁢󠁳󠁣󠁴󠁿,🏴󠁧󠁢󠁷󠁬󠁳󠁿)
so flags can't pass isCombinedIntoEmoji neither isSimpleEmoji...
I just want to block emoji input, so i adjust isSimpleEmoji with only checking isEmojiPresentation, not it's count == 1

thanks for sharing @krummler 🙏🙏 it's really help to me ☺️

@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