问题
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"
.
回答1:
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()
.
回答2:
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}
回答3:
With Swift 5, you can use one of the two following ways in order to get the description of an Emoji character.
#1. Using Unicode.Scalar.Properties
's name
property
Unicode.Scalar.Properties
has a name property. name
has the following declaration:
var name: String? { get }
The published name of the scalar.
The Playground code sample below shows how to use name
in order to get the published name of a Unicode scalar:
let emoji: Character = "😄"
for scalar in emoji.unicodeScalars {
print(scalar.properties.name)
}
/*
prints: Optional("SMILING FACE WITH OPEN MOUTH AND SMILING EYES")
*/
#2. Using Unicode.Scalar.Properties
's nameAlias
property
Unicode.Scalar.Properties
also has a property called nameAlias with the following declaration:
var nameAlias: String? { get }
The normative formal alias of the scalar. [...] The
nameAlias
property is provided to issue corrections if a name was issued erroneously. For example, the name of U+FE18 is “PRESENTATION FORM FOR VERTICAL RIGHT WHITE LENTICULAR BRAKCET” (note that “BRACKET” is misspelled). ThenameAlias
property then contains the corrected name.
The Playground sample codes below show the difference between name
and nameAlias
for U+FE18 Unicode scalar:
let emoji: Character = "\u{FE18}" // ︘
for scalar in emoji.unicodeScalars {
print(scalar.properties.name)
}
/*
prints:
Optional("PRESENTATION FORM FOR VERTICAL RIGHT WHITE LENTICULAR BRAKCET")
*/
let emoji: Character = "\u{FE18}" // ︘
for scalar in emoji.unicodeScalars {
print(scalar.properties.nameAlias)
}
/*
prints:
Optional("PRESENTATION FORM FOR VERTICAL RIGHT WHITE LENTICULAR BRACKET")
*/
回答4:
Swift 3 version of Eric Aya's answer:
let s = "This is a 😄"
if let result = s.applyingTransform(
kCFStringTransformToUnicodeName as StringTransform,
reverse: false) {
print(result)
}
This is a \N{SMILING FACE WITH OPEN MOUTH AND SMILING EYES}
来源:https://stackoverflow.com/questions/24699576/get-description-of-emoji-character