我在iOS中使用了一些需要通过其Unicode字符串来识别图标的字体图标。我希望能够从服务器以JSON格式检索代码,并动态创建此Unicode字符串以检索字体图标并将其放入标签等中。但是,我在转换方面遇到了问题。根据以下代码,有什么想法吗?
这里是工作示例,其中“E539”是从服务器接收到的字符串。我可以硬编码它并且它可以工作,但是动态创建它并不那么容易。
我找到了一个类,可以使用UTF32Char生成Unicode。这行代码能够工作,但不是整个解决方案。
尝试根据我找到的代码进行拼接,几乎可以工作,但是生成了错误的Unicode编码。我不知道为什么。
这里是工作示例,其中“E539”是从服务器接收到的字符串。我可以硬编码它并且它可以工作,但是动态创建它并不那么容易。
[iconLabel materialIconWithUnicodeStr:[NSString stringWithFormat:@"\uE539"]];
这些东西不起作用:
[iconLabel materialIconWithUnicodeStr:[NSString stringWithFormat:@"\u%@", @"E539"]];
[iconLabel materialIconWithUnicodeStr:[NSString stringWithFormat:@"\\u%@", @"E539"]];
我找到了一个类,可以使用UTF32Char生成Unicode。这行代码能够工作,但不是整个解决方案。
[iconLabel materialIconWithUnicodeStr:[EntypoStringCreator stringForIcon:0xE539]]
尝试根据我找到的代码进行拼接,几乎可以工作,但是生成了错误的Unicode编码。我不知道为什么。
NSString *unicodeStr = @"E539";
// attempt to convert E539 -> 0xE539
UTF32Char outputChar;
if ([unicodeStr getBytes:&outputChar maxLength:4 usedLength:NULL encoding:NSUTF32LittleEndianStringEncoding options:0 range:NSMakeRange(0, 1) remainingRange:NULL]) {
outputChar = NSSwapLittleIntToHost(outputChar); // swap back to host endian
// outputChar now has the first UTF32 character
}
// this does not give the correct icon at all
[iconLabel [EntypoStringCreator stringForIcon:outputChar]];