Each Emoji has a description that you can see in Mac OS's ⌃⌘Space
special character picker. There's a list of them here. Is there a way for me to query for this description in code (short of entering them all into a Struct)?
I'd like to do something like:
let 😄: Character = "😄"
let 😄desc: String = 😄.description
and have 😄desc
resolve to "SMILING FACE WITH OPEN MOUTH AND SMILING EYES"
.
Because emoji characters are treated as pictographs, they are encoded in Unicode based primarily on their general appearance, not on an intended semantic. The meaning of each emoji can vary depending on language, culture, context, and may change or be repurposed by various groups over time.
These characters require an "escape character" within the text which means these characters count as two characters in a message. Emojis or Unicode. Standard emojis count as two characters when writing a message.
Single-Character EmojiAn Emoji character reproduced by a single Unicode Scalar. Unicode defines Emoji Character as: emoji_character := \p{Emoji} But it doesn't necessarily mean that such a character will be drawn as an Emoji.
Emoji (from the Japanese e, “picture,” and moji, “character”) are a slightly more recent invention. Not to be confused with their predecessor, emoji are pictographs of faces, objects, and symbols.
The Core Foundation function CFStringTransform()
has transformations that
determine the Unicode standard name for special characters. Example:
let c : Character = "😄"
let cfstr = NSMutableString(string: String(c)) as CFMutableString
var range = CFRangeMake(0, CFStringGetLength(cfstr))
CFStringTransform(cfstr, &range, kCFStringTransformToUnicodeName, false)
print(cfstr)
Output:
\N{SMILING FACE WITH OPEN MOUTH AND SMILING EYES}
See http://nshipster.com/cfstringtransform/ for more information about
CFStringTransform()
.
Martin R's answer using Core Foundation's CFStringTransform()
still works, but the key feature actually comes from kCFStringTransformToUnicodeName
, and in Swift 2 we can use it simply like this, by bridging with NSString
and calling stringByApplyingTransform
:
let c: Character = "😄"
if let result = (String(c) as NSString)
.stringByApplyingTransform(
String(kCFStringTransformToUnicodeName),
reverse: false) {
print(result)
}
\N{SMILING FACE WITH OPEN MOUTH AND SMILING EYES}
The same for a String:
let s: String = "This is a 😄"
if let result = (s as NSString)
.stringByApplyingTransform(
String(kCFStringTransformToUnicodeName),
reverse: false) {
print(result)
}
This is a \N{SMILING FACE WITH OPEN MOUTH AND SMILING EYES}
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