SBGhatam
SBGhatam

Reputation: 3

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

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] stringByReplacingOccurrencesOfString:@"\\\\u" withString:@"\\u"]];

Note: Assigning the following string does the trick:

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

Upvotes: 0

Views: 3372

Answers (2)

SBGhatam
SBGhatam

Reputation: 3

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.

Upvotes: 0

chown
chown

Reputation: 52738

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

Upvotes: 1

Related Questions