I'm building an iOS App, and Emojis play a big part in it.
In iOS 10.2, new emojis were released.
I'm pretty sure that if someone has iOS 8, for example, they wouldn't actually be able to see these emojis. Is there a way to detect this? I'm trying to dynamically build a list of all the Emojis that are supported on the user's iOS version, but I'm having a bit of trouble.
If you don't see the emoji keyboard, make sure that it's added. Go to Settings > General and tap Keyboard. Tap Keyboards, then tap Add New Keyboard. Tap Emoji.
Emoji are created by the Unicode Consortium and they are a part of the “Unicode” standard. That simply means emoji are essentially a standard that anyone can incorporate into their product. That's why every operating system has the same emoji.
Today Apple has released iOS 14.6, just a few weeks after unveiling a large number of new emojis in iOS 14.5. While today's update doesn't feature any further new emojis, it does update apperance of the people climbing emojis, and could well be the last Apple emoji update before Unicode's 2021 recommendations are made.
Clarification: an Emoji is merely a character in the Unicode Character space, so the present solution works for all characters, not just Emoji.
To know if a Unicode character (including an Emoji) is available on a given device or OS, run the unicodeAvailable()
method below.
It works by comparing a given character image against a known undefined Unicode character U+1FFF
.
Character
extensionprivate static let refUnicodeSize: CGFloat = 8 private static let refUnicodePng = Character("\u{1fff}").png(ofSize: Character.refUnicodeSize) func unicodeAvailable() -> Bool { if let refUnicodePng = Character.refUnicodePng, let myPng = self.png(ofSize: Character.refUnicodeSize) { return refUnicodePng != myPng } return false }
All characters will be rendered as a png at the same size (8) as defined once in
static let refUnicodeSize: CGFloat = 8
The undefined character U+1FFF
image is calculated once in
static let refUnicodePng = Character("\u{1fff}").png(ofSize: Character.refUnicodeSize)
A helper method optionally creates a png from a Character
func png(ofSize fontSize: CGFloat) -> Data?
let codes:[Character] = ["\u{2764}","\u{1f600}","\u{1F544}"] // ❤️, 😀, undefined for unicode in codes { print("\(unicode) : \(unicode.unicodeAvailable())") }
func unicodeRange(from: Int, to: Int) { for unicodeNumeric in from...to { if let scalar = UnicodeScalar(unicodeNumeric) { let unicode = Character(scalar) let avail = unicode.unicodeAvailable() let hex = String(format: "0x%x", unicodeNumeric) print("\(unicode) \(hex) is \(avail ? "" : "not ")available") } } }
Character
to png func png(ofSize fontSize: CGFloat) -> Data? { let attributes = [NSAttributedStringKey.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 = UIImagePNGRepresentation(charImage) } UIGraphicsEndImageContext() return png }
► Find this solution on GitHub and a detailed article on Swift Recipes.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With