我有一个UILabel
我要在其中显示Unicode字符。以下作品:
label.text = @"\U000025A0";
现在我想从十进制数中动态生成Unicode字符,如下所示:
label.text = NSString stringWithFormat:@"\U%08x",9632];
Xcode编译器因以下错误而失败:
\U used with no following hex digits
有没有办法将十进制数9632转换为Unicode,然后将其显示在UILabel
?
答案 0 :(得分:1)
我有同样的问题来显示entypo Font特殊字符,所以我实现了我自己的EntypoStringCreator
类(在SO的帮助下)。以下是将unicode号转换为nsstring的方法:
·H
@interface EntypoStringCreator : NSObject
+(NSString *)stringForIcon:(UTF32Char)char32;
@end
的.m
@implementation EntypoStringCreator
+(NSString *)stringForIcon:(UTF32Char)char32
{
if ((char32 & 0xFFFF0000) != 0)
return [self stringFromUTF32Char:char32];
else
return [self stringFromUTF16Char:(UTF16Char)(char32&0xFFFF)];
}
+(NSString *)stringFromUTF32Char:(UTF32Char)char32
{
char32 -= 0x10000;
unichar highSurrogate = (unichar)(char32 >> 10); // leave the top 10 bits
highSurrogate += 0xD800;
unichar lowSurrogate = char32 & 0x3FF; // leave the low 10 bits
lowSurrogate += 0xDC00;
return [NSString stringWithCharacters:(unichar[]){highSurrogate, lowSurrogate} length:2];
}
+(NSString *)stringFromUTF16Char:(UTF16Char)char16
{
return [NSString stringWithCharacters:(unichar[]){char16} length:1];
}
@end
答案 1 :(得分:-1)
试试这个::
NSString *myString = [NSString stringWithUTF8String:"0xF09F948A"]; <-- UTF-8 Hexadecimal Encoding
myLabel.text = myString;