尝试使用StringWithFormat在UILabel中显示表情符号

Hal*_*lpo 2 unicode xcode objective-c emoji

我试图从数组中获取随机的表情符号字符。我如何在不适合XCode的情况下将这些值转换为Unicode格式?:

label.text = [NSString stringWithFormat:@"\U%@",[emojis randomObject]];
Run Code Online (Sandbox Code Playgroud)

我得到:\U used with no following hex digits每次。

您不能构造像这样的unicode格式吗?

Hal*_*lpo 5

这是我的解决方案:

NSString *code = emojArray[i];
NSScanner *hexScan = [NSScanner scannerWithString:code];
unsigned int hexNum;
[hexScan scanHexInt:&hexNum];
UTF32Char inputChar = hexNum;
NSString *res = [[NSString alloc] initWithBytes:&inputChar length:4 encoding:NSUTF32LittleEndianStringEncoding];
Run Code Online (Sandbox Code Playgroud)

res,当显示在UILabel中时,现在是表情符号。