开发者

UILabel to display Emoji emotions rather than their unicode values \ueXXXX

开发者 https://www.devze.com 2023-04-09 22:29 出处:网络
I\'m retrieving values from a server in XML format, the server sends the following string: \"This is a nice string with Emoji \\ue056\\ue056\\ue056\\ue057\\ue057\\ue056\\ue056\"

I'm retrieving values from a server in XML format, the server sends the following string: "This is a nice string with Emoji \ue056\ue056\ue056\ue057\ue057\ue056\ue056"

I have been struggling to make my UILabel display the emoji icons rather the following

\ue056\ue056\ue056\ue057\ue057\ue056\ue056

I have tried

[chatText setText:[chatString stringByReplacingOccurrencesOfString:@"\\\\u" withString:@"\\u"]];

Not much luck. Being desperate I have done the following:

[chatText setText:[[chatString cStringUsingEncoding:NSNonLossyASCIIStringEncoding] stringByReplacingOccurrences开发者_如何学PythonOfString:@"\\\\u" withString:@"\\u"]];

Note: Assigning the following string does the trick:

[chatText setText:@"\ue056\ue056\ue056\ue057\ue057\ue056\ue056"];


It looks like you would need to go through each of the characters, and use stringWithFormat:@"%C" for each of the emoji characters:

// Loop through string by characters, and then do something like:
// NSString *emoji = [NSString stringWithFormat:@"%C", 16-bit Unicode character (unichar) here];
NSString *emoji = [NSString stringWithFormat:@"%C", 0xe415];
chatText.text = [chatText.text stringByAppendingString:emoji];

Check out the String Format Specifiers


I have built a stringHandler class that contains the following function to overcome this issue, which pretty much solved my problem:

+ (NSString *) mapEmojiFromServer:(NSString *) inString
{
    inString = [inString stringByReplacingOccurrencesOfString:@"\\" withString:@""];
    inString = [inString stringByReplacingOccurrencesOfString:[@"ue001" lowercaseString] withString:@"\ue001"];
.
.
.

    return inString;
}

I haven't noticed a performance issue other than the IDE getting crazy when navigating and scrolling through the page that contains this 2000 lines function

Thanks a lot for your help.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号