アイコンをUnicode文字列で識別する必要があるiosでフォントアイコンを使用しています。 JSONとしてサーバーからコードを取得し、このユニコード文字列を動的に作成してフォントアイコンを取得し、ラベルなどを入れたいと思っています。しかし、私は変換を行うのに問題があります。以下のコードに基づいて任意のアイデア?ios dynamic unicodeフォントアイコンのNSString
"E539"はサーバーから受信した文字列です。私はそれをハードコードすることができ、それは動作しますが、それを動的に作成することはそれほど簡単ではありません。
[iconLabel materialIconWithUnicodeStr:[NSString stringWithFormat:@"\uE539"]];
物事のこれらのタイプは動作しません:
[iconLabel materialIconWithUnicodeStr:[NSString stringWithFormat:@"\u%@", @"E539"]];
[iconLabel materialIconWithUnicodeStr:[NSString stringWithFormat:@"\\u%@", @"E539"]];
私はユニコードを作るためにUTF32Charを取るだろう、私はここで見つけるクラスを持っています。この行は機能しますが、全体的な解決策ではありません。
私が見つけたコードからは盲目的にまとめると、ほとんどうまくいくが、間違ったユニコードが作成される。なぜ私は考えていない。
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]];
なぜ数にhexcodeを変換すると「%のC」の文字列に追加していませんか? – Eiko