samedi 25 juin 2016
ios dynamic unicode NSString for font icons
I'm using some font icons in ios that require identifying the icon by its unicode string. I'd like to be able to retreive the code from my server as JSON, and dynamically create this unicode string to retrieve the font icon and put in the label, etc. I'm having trouble doing the conversions however. Any ideas based on below code?
The working example is here, where "E539" is the string received from the server. I can hardcode it and it works, but creating it dynamically is not so easy.
[iconLabel materialIconWithUnicodeStr:[NSString stringWithFormat:@"uE539"]];
These types of things don't work:
[iconLabel materialIconWithUnicodeStr:[NSString stringWithFormat:@"u%@", @"E539"]];
[iconLabel materialIconWithUnicodeStr:[NSString stringWithFormat:@"\u%@", @"E539"]];
I have a class I found here that would take UTF32Char to make the unicode. This line works, but isn't the whole solution.
[iconLabel materialIconWithUnicodeStr:[EntypoStringCreator stringForIcon:0xE539]]
Trying to piece it together, rather blindly from code I found, almost works, but creates the wrong unicode. I have no idea why.
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]];
Inscription à :
Publier les commentaires (Atom)
Aucun commentaire:
Enregistrer un commentaire