UILabel not showing emoji - ios

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.

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?

Try to set your UILabel with:
//Example
[NSString stringWithUTF8String:"\ud83d"]

Related

How to show special character in UILabel iOS

I am trying to implement an app where I would like to show some text in Spanish format. For example I would like to show "España" but in my label it shows "Espa√ɬ±a" and also it changes the text for some of other text.
How to get rid of these. If anybody could help. Thanks.
Edit: When i am getting my response it logs that Below result
Message = (
"Espa\U221a\U00c9\U00ac\U00b1a:1.3\U221a\U00c7\U00ac\U00a2/min"
);
But when i extract the value according to key from Dictionary it shows
España:1.3¢/min
It means when i am getting the value from dictionary it cant do proper decoding.
how to resolve this. Any idea..?
First convert your response String to NSData using NSUTF8StringEncoding encoding, then again convert the same data to finalString like below.
NSString *string = #"España"; //Your response String goes here
NSData *data = [string dataUsingEncoding:NSUTF8StringEncoding];
NSString *finalString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
lblTemp.text = finalString;
UPDATE 1
I think there is some error from your response, Please see below
NSString *string = #"Nu\\u0161a Florjan\\u010di\\u010d";
NSString *finalString = [NSString
stringWithCString:[string cStringUsingEncoding:NSUTF8StringEncoding]
encoding:NSNonLossyASCIIStringEncoding];
NSLog(#"finalString = %#", finalString);
Output of above code is,
finalString = Nuša Florjančič
UPDATE 2
If you want output string like "España", your desired response should be "Espa\u00F1a", Find below,
NSString *string = #"Espa\\u00F1a";
NSString *finalString = [NSString
stringWithCString:[string cStringUsingEncoding:NSUTF8StringEncoding]
encoding:NSNonLossyASCIIStringEncoding];
NSLog(#"%#",finalString);
Output is España

unable to show emoji's properly in UIlabel

I am trying to save the emojis to server and on later time receiving them.
I used the Following Code before
NSData *data = [strEmo dataUsingEncoding:NSNonLossyASCIIStringEncoding];
NSString *goodValue = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSData *data1 = [strEmo dataUsingEncoding:NSUTF8StringEncoding];
NSString *goodValue = [[NSString alloc] initWithData:data encoding:NSNonLossyASCIIStringEncoding];
It generates the Hashcode for the Emoji. But decoding doesn't work. So I skipped the Idea of using this Code.
Then I used a third Party NSString+HTML.h Class. Which Sends and recieve emoji easily. But
Now the Problem is when there are so many emoji's the UIlabel on which I am showing the data, the emojis are distorted and If I saved 20 emojis it shows 12-13 only.
I have added the Pic for reference
where Yellow part is UIlabel with back colored Yellow
Buddy why are you changing the string in Data two times in a row just simply use this
NSString *uniText = [NSString stringWithUTF8String:[strEmo UTF8String]];
NSData *msgData = [uniText dataUsingEncoding:NSNonLossyASCIIStringEncoding];
NSString *readyString = [[NSString alloc] initWithData:msgData encoding:NSUTF8StringEncoding];
First convert your string into constant C characters then convert it into string using UTF8 encoding, now convert it into NSData as (7-bit verbose ASCII to represent all Unicode characters) using NSNonLossyASCIIStringEncoding encoding and then again string ready to send with unicode characters. Hope this should work as it is working fine in my code.

NSURL string content to a label

Im trying to read some values from a local url when i press a button and i send the value to a label. as soon as i launch the app and i press the button for the first time i get the value. but the second time i press the button the label get no text even though the NSLog message continues to display the value. any idea?
here is the code:
- (IBAction)refresh:(id)sender {
NSArray *listItems;
NSString *msg;
NSStringEncoding encoding;
NSURL *url = [NSURL URLWithString:#"http://192.168.1.177"];
NSData *data = [NSData dataWithContentsOfURL:url];
msg = [[NSString alloc] initWithData:data encoding:encoding];
listItems = [msg componentsSeparatedByString:#" "];
label.text = listItems[0];
NSLog(listItems[0]);
}
Your encoding variable is never initialized with a value, so it has the default value of 0, which is not a valid value of NSStringEncoding according to NSString.h. If you run the static analyzer (Command-Shift-B in Xcode), you will get a message that has an uninitialized value when it is used.
Change this line:
msg = [[NSString alloc] initWithData:data encoding:encoding];
to this:
msg = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
(Substitute the appropriate encoding, but NSUTF8StringEncoding is a good first guess.)
You should also use a format string to log:
NSLog(#"%#", listItems[0]);

convert unicode characters while data loading in uiwebview

{ disclaimertxt = "<b>sample tex\U221a\U00a9t n\U221a\U00a9ewerv\U221a\U00a9e iew adults.</b>
\n<br/>
\n<br/>
\nthis is sample \U221a\U2020 an d\U221a\U00a9convertion of the language\U221a\U00a9reduction\U201a\U00c4\U00b6
\n ";
}
the above one is dictionary which contains above value with key disclaimertext
but in output the unicode characters is replaced with ?(C)" and "?A
and my code is :
NSData *messagedata = [dictionary[disclaimertxt] dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *string = [[NSString alloc]initWithData:messagedata encoding:NSUTF8StringEncoding];
so i want to display those unicode characters with respected text/value while loading in uiwebview.. plz give ur suggestions. in ios7 its working fine with desired output what i need. but when i run ios6 aim getting above outout.so plz find solution.
Follow these steps:
manually load the HTML from the page that doesn't include the meta tag, into a string.
Using string manipulation, insert your appropriate encoding meta tag into the manually loaded HTML
set the HTML in your web view to the modified string using loadHTMLString:
Use this to convert Unicode charaters to NSString:
char cString[] = "This isn\u2019t Test String";
NSData *data = [NSData dataWithBytes:cString length:strlen(cString)];
NSString *string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
Now use this string in your Webview.

Convert NSString to UTF8String?

Currently I am download data from a server and I have this line to get that NSData into a NSString.
NSString *txt = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
How would I go from here to convert that NSString into a UTF8 string?
Thanks!
NSData *utf8Data = [txt dataUsingEncoding:NSUTF8StringEncoding];
Change NSASCIIStringEncoding to NSUTF8StringEncoding .
I ran a quick test now with some dummy data and seemed to work fine with just that one line.

Resources