Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Description of Emoji Character

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".

like image 835
davextreme Avatar asked Jul 11 '14 14:07

davextreme


People also ask

Do emojis have Unicode values?

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.

Is an emoji a character?

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.

What is a single emoji character?

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.

Is an emoji a pictograph?

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.


2 Answers

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().

like image 153
Martin R Avatar answered Sep 22 '22 05:09

Martin R


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}

like image 28
Eric Aya Avatar answered Sep 19 '22 05:09

Eric Aya