UILabel not showing emoji

2019-04-09 13:36发布

问题:

I'm facing a problem about displaying emoji in a UILabel. I get this data from a web service (i cannot change the way i get the data) : This value is from the debugger :

__NSCFString *  @"emoji \\ud83d\\ude1b\\ud83d\\ude1d"   0x000000017405ea80 

Value from NSLog :

emoji \ud83d\ude1b\ud83d\ude1d

if i assign this value to my UILabel text property i get on the screen :

emoji \ud83d\ude1b\ud83d\ude1d

I tried to encode and decode the string using :

NSData *data = [string dataUsingEncoding:NSNonLossyASCIIStringEncoding];
NSString *dataValue = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
return dataValue;

and when i try :

NSData *data = [string dataUsingEncoding:NSUTF8StringEncoding];
NSString *output = [[NSString alloc] initWithData:data encoding:NSNonLossyASCIIStringEncoding];

i get the emoji on the screen but if a string does not have emoji inside it will be nil and get empty label on device.

I can't get it to work correctly.

Thanks for any help.

回答1:

I tried your code, the second one.. and it is working perfectly:

Here is what i have:

and the output:

removing the emoji string \\ud83d\\ude1b\\ud83d\\ude1d just leave emoji. this is using simulator, i wonder the error only appear on the device? while in simulator it's working?



回答2:

Try to set your UILabel with:

//Example
[NSString stringWithUTF8String:"\ud83d"]