I want to be able to get an array of all the available emojis in Swift. What I mean by available is all the ones that are current accessable to the device. Like when Apple adds some new emojis, I would like to not have to change any code, and the new emojis just get added in the array with all the other ones.
If getting updated emojis is not possible, then how can I get all of the current emojis into an array (or perhaps a .plist file I can then load into an array) so I can access them in code?
This SO seems to provide the answer
for i in 0x1F601...0x1F64F {
let c = String(UnicodeScalar(i) ?? "-")
print(c)
}
You should go beyond 1F64F,
https://stackoverflow.com/questions/30757193/find-out-if-character-in-string-is-emoji
0x1F600...0x1F64F, // Emoticons
8400...8447: // Combining Diacritical Marks for Symbols
9100...9300, // Misc items
0x2600...0x26FF, // Misc symbols
0x2700...0x27BF, // Dingbats
0xFE00...0xFE0F, // Variation Selectors
0x1F018...0x1F270, // Various asian characters
0x1F300...0x1F5FF, // Misc Symbols and Pictographs
0x1F680...0x1F6FF, // Transport and Map
0x1F1E6...0x1F1FF, // Regional country flags
0x1F900...0x1F9FF, // Supplemental Symbols and Pictographs
65024...65039, // Variation selector
However, you get some undefined (marked as ? at the end of ranges)
See here to skip them
https://stackoverflow.com/questions/41318999/is-there-a-way-to-know-if-an-emoji-is-supported-in-ios
Note, while searching, found this interesting link to generate emoji flags based on country code
www.timekl.com/blog/2017/08/31/swift-tricks-emoji-flags/
That ends up with the following code
func isEmoji(_ value: Int) -> Bool {
switch value {
case 0x1F600...0x1F64F, // Emoticons
0x1F300...0x1F5FF, // Misc Symbols and Pictographs
0x1F680...0x1F6FF, // Transport and Map
0x1F1E6...0x1F1FF, // Regional country flags
0x2600...0x26FF, // Misc symbols 9728 - 9983
0x2700...0x27BF, // Dingbats
0xFE00...0xFE0F, // Variation Selectors
0x1F900...0x1F9FF, // Supplemental Symbols and Pictographs 129280 - 129535
0x1F018...0x1F270, // Various asian characters 127000...127600
65024...65039, // Variation selector
9100...9300, // Misc items
8400...8447: // Combining Diacritical Marks for Symbols
return true
default: return false
}
}
extension Character {
private static let refUnicodeSize: CGFloat = 8
private static let refUnicodePng =
Character("\u{1fff}").png(ofSize: Character.refUnicodeSize)
func png(ofSize fontSize: CGFloat) -> Data? {
let attributes = [NSAttributedString.Key.font:
UIFont.systemFont(ofSize: fontSize)]
let charStr = "\(self)" as NSString
let size = charStr.size(withAttributes: attributes)
UIGraphicsBeginImageContext(size)
charStr.draw(at: CGPoint(x: 0,y :0), withAttributes: attributes)
var png:Data? = nil
if let charImage = UIGraphicsGetImageFromCurrentImageContext() {
png = charImage.pngData()
}
UIGraphicsEndImageContext()
return png
}
func unicodeAvailable() -> Bool {
if let refUnicodePng = Character.refUnicodePng,
let myPng = self.png(ofSize: Character.refUnicodeSize) {
return refUnicodePng != myPng
}
return false
}
}
for i in 8400...0x1F9FF where isEmoji(i) {
if let scalar = UnicodeScalar(i) {
let unicode = Character(scalar)
if unicode.unicodeAvailable() {
print(i, String(scalar))
count += 1
} else {
notAvail += 1
print(i)
}
} else {
notCounted += 1
}
}
print("Count", count, "Not counted", notCounted, "Not Avail", notAvail)