How could I get an array of emojis found in iOS8.3
After some struggling,here is the fullest list of emojis found in iOS8.3 written in Swift.Browse in Safari for best compatibility.[April 8 2015]
let allEmojis = [",",",",",",",",",",",","☺️",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",","❤️,"❤️,","❤️,"❤️,",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",","☝","☝,"☝,"☝,"☝,"☝,",",",",",",",",",",",",",",",",",",",",",",",",",",",",",","✌","✌,"✌,"✌,"✌,"✌,",",",",",","✊","✊,"✊,"✊,"✊,"✊,"✋","✋,"✋,"✋,"✋,"✋,",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",","⚡️",",","☀️","⛅️","☁️",",","☔️",","❄️",","⭐️",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",","☕️",",",",",",",",",",",",",",",",",",",",",","✨",",",",",",",",",","❤️",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",","⛄️",",",",",",",",",",",",",",",",",",","⛺️",","⚽️",",","⚾️",",","⛳️",",",",",",",",",",",",",",",",",",",",",",",",",",",","️",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",","⛽️",",",",",","✈️",","⚓️",",","⛵️",",",",",",",",",",",",",",",","⛲️",",",",",",",",",",",",",",",",",",","⛪️",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",","⌚️",",",","⏰","⏳","⌛️",",",",",",",","☎️",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",","✉️",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",",","✂️",",",",",",","✒️","✏️",",",",",",",",",",",",",",",",",",",",","⛔️",",",",",",",",",",","㊙️","㊗️",",",",","️",",",",",",",","️",","❇️","✳️","❎","✅","✴️",",",",",",",",",",","️",",",",",",",",","♈️","♉️","♊️","♋️","♌️","♍️","♎️","♏️","♐️","♑️","♒️","♓️",",",",","♿️",",",","▶️","◀️",",","⏩","⏪","⏫","⏬","➡️","⬅️","⬆️","⬇️","↗️","↘️","↙️","↖️","↕️","↔️",","↪️","↩️","⤴️","⤵️",",",","#⃣","0⃣","1⃣","2⃣","3⃣","4⃣","5⃣","6⃣","7⃣","8⃣","9⃣",",",",",","ℹ️",",",","➕","➖","〰","➗","✖️","✔️",","™","©","®",",","➰","➿","〽️","❗️","❓","❕","❔","‼️","⁉️","❌","⭕️",",",",",",",","Ⓜ️","⛎",",",","⚠️","♨️","♻️",",","♠️","♣️","♥️","♦️","☑️","⚪️","⚫️",",",",",",",",",","▪️","▫️","⬛️","⬜️","◼️","◻️","◾️","◽️",",",",",",",",",",",",",",",",",",",",",",",",",","]
Also you could write them into a plist file for persistence.
(allEmojis as NSArray).writeToFile(NSHomeDirectory() + "/emoji.plist", atomically: true)
How to get all available emojis in an array in Swift?
This SO seems to provide the answer
How to list (almost) all emojis in Swift for iOS 8 without using any form of lookup tables?
for i in 0x1F601...0x1F64F {
let c = String(UnicodeScalar(i) ?? "-")
print(c)
}
You should go beyond 1F64F,
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
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)
How to list (almost) all emojis in Swift for iOS 8 without using any form of lookup tables?
You can loop over those hex values with a Range
: 0x1F601...0x1F64F
and then create the String
s using a UnicodeScalar
:
for i in 0x1F601...0x1F64F {
guard let scalar = UnicodeScalar(i) else { continue }
let c = String(scalar)
print(c)
}
Outputs:
/p>
If you want all the emoji, just add another loop over an array of ranges:
// NOTE: These ranges are still just a subset of all the emoji characters;
// they seem to be all over the place...
let emojiRanges = [
0x1F601...0x1F64F,
0x2702...0x27B0,
0x1F680...0x1F6C0,
0x1F170...0x1F251
]
for range in emojiRanges {
for i in range {
guard let scalar = UnicodeScalar(i) else { continue }
let c = String(scalar)
print(c)
}
}
For those asking, the full list of available emojis can be found here: https://www.unicode.org/emoji/charts/full-emoji-list.html
A parsable list of unicode sequences for all emojis can be found in the emoji-sequences.txt
file under the directory for the version you're interested in here: http://unicode.org/Public/emoji/
As of 9/15/2021 the latest version of the emoji standard available on Apple devices is 13.1.
Find out if Character in String is emoji?
What I stumbled upon is the difference between characters, unicode scalars and glyphs.
For example, the glyph consists of 7 unicode scalars:
- Four emoji characters: /li>
- In between each emoji is a special character, which works like character glue; see the specs for more info
Another example, the glyph consists of 2 unicode scalars:
- The regular emoji: /li>
- A skin tone modifier: /li>
Last one, the glyph 1️⃣ contains three unicode characters:
- The digit one:
1
- The variation selector
- The Combining Enclosing Keycap:
⃣
So when rendering the characters, the resulting glyphs really matter.
Swift 5.0 and above makes this process much easier and gets rid of some guesswork we needed to do. Unicode.Scalar
's new Property
type helps is determine what we're dealing with.
However, those properties only make sense when checking the other scalars within the glyph. This is why we'll be adding some convenience methods to the Character class to help us out.
For more detail, I wrote an article explaining how this works.
For Swift 5.0, this leaves you with the following result:
extension Character {
/// A simple emoji is one scalar and presented to the user as an Emoji
var isSimpleEmoji: Bool {
guard let firstScalar = unicodeScalars.first else { return false }
return firstScalar.properties.isEmoji && firstScalar.value > 0x238C
}
/// Checks if the scalars will be merged into an emoji
var isCombinedIntoEmoji: Bool { unicodeScalars.count > 1 && unicodeScalars.first?.properties.isEmoji ?? false }
var isEmoji: Bool { isSimpleEmoji || isCombinedIntoEmoji }
}
extension String {
var isSingleEmoji: Bool { count == 1 && containsEmoji }
var containsEmoji: Bool { contains { $0.isEmoji } }
var containsOnlyEmoji: Bool { !isEmpty && !contains { !$0.isEmoji } }
var emojiString: String { emojis.map { String($0) }.reduce("", +) }
var emojis: [Character] { filter { $0.isEmoji } }
var emojiScalars: [UnicodeScalar] { filter { $0.isEmoji }.flatMap { $0.unicodeScalars } }
}
Which will give you the following results:
"A̛͚̖".containsEmoji // false
"3".containsEmoji // false
"A̛͚̖▶️".unicodeScalars // [65, 795, 858, 790, 9654, 65039]
"A̛͚̖▶️".emojiScalars // [9654, 65039]
"3️⃣".isSingleEmoji // true
"3️⃣".emojiScalars // [51, 65039, 8419]
"quot;.isSingleEmoji // true
"♂️".isSingleEmoji // true
"quot;.isSingleEmoji // true
"⏰".isSingleEmoji // true
"quot;.isSingleEmoji // true
"quot;.isSingleEmoji // true
"quot;.isSingleEmoji // true
"quot;.containsOnlyEmoji // true
"quot;.containsOnlyEmoji // true
"Hello quot;.containsOnlyEmoji // false
"Hello quot;.containsEmoji // true
" Héllo quot;.emojiString // "quot;
"quot;.count // 1
" Héllœ quot;.emojiScalars // [128107, 128104, 8205, 128105, 8205, 128103, 8205, 128103]
" Héllœ quot;.emojis // ["quot;, "quot;]
" Héllœ quot;.emojis.count // 2
"quot;.isSingleEmoji // false
"quot;.containsOnlyEmoji // true
For older Swift versions, check out this gist containing my old code.
Is there a way to know if an Emoji is supported in iOS?
Clarification: an Emoji is merely a character in the Unicode Character space, so the present solution works for all characters, not just Emoji.
Synopsis
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
.
unicodeAvailable(), a Character
extension
private 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
}
Discussion
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 instatic 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?
1. Example: Test against 3 emoji
let codes:[Character] = ["\u{2764}","\u{1f600}","\u{1F544}"] // ❤️, , undefined
for unicode in codes {
print("\(unicode) : \(unicode.unicodeAvailable())")
}
2. Example: Test a range of Unicode characters
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")
}
}
}
Helper function: 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.
Not showing smily ( Emoji ) in in UITextView in iOS?
The \uxxxx
notation is only interpreted by the compiler (as the source code is usually in ASCII or MacRoman or whatever but not often UTF8)
Plist files uses the characters directly, and are encoded in UTF8.
So you should insert the emoji character itself into the plist directly, instead of using the \uxxxx
notation, as the Plist data will be read as-is.
Lion and Mountain Lion Keyboard palettes contains emoji characters directly, so that should not be difficult to insert the characters when editing the PLIST anyway.
Detect if a user has typed an emoji character in UITextView
Over the years these emoji-detecting solutions keep breaking as Apple adds new emojis w/ new methods (like skin-toned emojis built by pre-cursing a character with an additional character), etc.
I finally broke down and just wrote the following method which works for all current emojis and should work for all future emojis.
The solution creates a UILabel with the character and a black background. CG then takes a snapshot of the label and I scan all pixels in the snapshot for any non solid-black pixels. The reason I add the black background is to avoid issues of false-coloring due to Subpixel Rendering
The solution runs VERY fast on my device, I can check hundreds of characters a second, but it should be noted that this is a CoreGraphics solution and should not be used heavily like you could with a regular text method. Graphics processing is data heavy so checking thousands of characters at once could result in noticeable lag.
-(BOOL)isEmoji:(NSString *)character {
UILabel *characterRender = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 1, 1)];
characterRender.text = character;
characterRender.backgroundColor = [UIColor blackColor];//needed to remove subpixel rendering colors
[characterRender sizeToFit];
CGRect rect = [characterRender bounds];
UIGraphicsBeginImageContextWithOptions(rect.size,YES,0.0f);
CGContextRef contextSnap = UIGraphicsGetCurrentContext();
[characterRender.layer renderInContext:contextSnap];
UIImage *capturedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
CGImageRef imageRef = [capturedImage CGImage];
NSUInteger width = CGImageGetWidth(imageRef);
NSUInteger height = CGImageGetHeight(imageRef);
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
unsigned char *rawData = (unsigned char*) calloc(height * width * 4, sizeof(unsigned char));
NSUInteger bytesPerPixel = 4;
NSUInteger bytesPerRow = bytesPerPixel * width;
NSUInteger bitsPerComponent = 8;
CGContextRef context = CGBitmapContextCreate(rawData, width, height,
bitsPerComponent, bytesPerRow, colorSpace,
kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
CGColorSpaceRelease(colorSpace);
CGContextDrawImage(context, CGRectMake(0, 0, width, height), imageRef);
CGContextRelease(context);
BOOL colorPixelFound = NO;
int x = 0;
int y = 0;
while (y < height && !colorPixelFound) {
while (x < width && !colorPixelFound) {
NSUInteger byteIndex = (bytesPerRow * y) + x * bytesPerPixel;
CGFloat red = (CGFloat)rawData[byteIndex];
CGFloat green = (CGFloat)rawData[byteIndex+1];
CGFloat blue = (CGFloat)rawData[byteIndex+2];
CGFloat h, s, b, a;
UIColor *c = [UIColor colorWithRed:red green:green blue:blue alpha:1.0f];
[c getHue:&h saturation:&s brightness:&b alpha:&a];
b /= 255.0f;
if (b > 0) {
colorPixelFound = YES;
}
x++;
}
x=0;
y++;
}
return colorPixelFound;
}
Related Topics
How to Debug "Terminated Due to Memory Error"
Swift - Get Local Date and Time
Xcode 10.2 with Swift 5.0 Compiler - Protocol Inheritance Issue
Right Way of Determining Internet Speed in iOS 8
Preferredinterfaceorientationforpresentation Must Return a Supported Interface Orientation
How to Install iOS 7 and Onwards Simulators in Xcode 7 Beta 5
Swift: How to Open a New App When Uibutton Is Tapped
Ios: Launch Image Multiple Language
Indexpathforcell Returns Nil Since iOS7
iOS 9 Segue Causes App to Freeze (No Crash or Error Thrown)
When to Use Takeunretainedvalue() or Takeretainedvalue() to Retrieve Unmanaged Objects in Swift
Waiting for Multiple Asynchronous Download Tasks
Centering Subview's X in Autolayout Throws "Not Prepared for the Constraint"
Custom Font Not Working Programmatically in Swift