4

In my app when i tried to set the attached string to the UIlabel special characters are not displaying. Please help me in solving this issue.

Thanks

Result

Special Character

Saikiran Komirishetty
  • 6,525
  • 1
  • 29
  • 36

4 Answers4

1

If you have a Euro character on your keyboard, you can use it directly:

[lLabel setText:@"€"];

Otherwise use the Unicode escape:

[lLabel setText:@"\u20AC"];

ADDED The Foundation framework and the NSString class does not know escaping sequences like "¿euro¿", so you have to replace it manually:

NSString *s = @"abc ¿euro¿ def";
NSString *d = [s stringByReplacingOccurrencesOfString:@"¿euro¿" withString:@"\u20AC"];
Martin R
  • 529,903
  • 94
  • 1,240
  • 1,382
  • special symbols like euro,inverted question mark are coming from the web service for this i have used utf8 string encoding. So when i tried to display them these characters are not displaying so in this case i cannot hard code these values as these values are dynamic. For the above case inverted question mark is not getting displayed I have used the following link but this is not working http://www.iphonedevsdk.com/forum/iphone-sdk-development/32878-displaying-special-characters-with-proper-encoding.html. Could you please tell me how to solve this issue – Saikiran Komirishetty Sep 24 '12 at 11:11
  • @Saikiran: See my updated answer. You should also make sure that the string from your web service really uses the "Inverted Question Mark" character (Unicode U+00BF) and not something else that is just displayed as a inverted question mark. – Martin R Sep 24 '12 at 11:24
  • :I am surprised to see that in the database i set euro symbol as the custom name but in the web service response i am receiving the inverted question mark instead of euro symbol and i am unable to replace this inverted question mark with unicode character which you have mentioned – Saikiran Komirishetty Sep 24 '12 at 11:41
  • @Saikiran: I assume that the character is just *displayed* as inverted question mark. You have to find out what characters your web service string exactly uses. – Martin R Sep 24 '12 at 11:47
  • still no solution.. tried different encoding styles but did not find any solution – Saikiran Komirishetty Sep 27 '12 at 06:30
0

Covert the string to UTF-8 and then set it to label text.

[label setText:[NSString stringWithFormat:@"%s",[@"your_string" UTF8String]]];

[EDIT]

I tested this and works fine without converting string to UTF-8!

label.text = @"¿euro¿";

Check this output

Please make sure that your server's response set to UTF-8 and it should fix it. I had the same issue where I was getting strings from server in UTF8 - NSUTF8StringEncoding! I changed it and everything else worked fine!

For example I do it like:

ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
request.defaultResponseEncoding = NSUTF8StringEncoding;
AVerguno
  • 1,277
  • 11
  • 27
Paresh Masani
  • 7,474
  • 12
  • 73
  • 139
  • 1
    Both methods will not work. `dataUsingEncoding:` returns a `NSData *`, and `UTF8String` returns a `const char *`. But `[UILabel setText:]` requires a `NSString *` argument. – Martin R Sep 24 '12 at 11:03
  • @Martin I have been using this but not directly on the string but strings returned from the server and it's working fine. Make sure that you set server response type to NSUTF8StringEncoding. Ideally when you print NSLog, it may print invalid characters but if you assign it to Label, it would display it correctly. – Paresh Masani Sep 24 '12 at 11:49
  • My point was that you cannot use the result of `dataUsingEncoding` and `UTF8String` as argument to `[UILabel setText:]`. The statements as you have written them will cause compiler warnings about incompatible types, and it crashes at runtime. – Martin R Sep 24 '12 at 12:03
  • You are right. I didn't test it before. Corrected the answer. You don't have to UTF-8 explicitly if you are getting string from the server. Just make server's response default encoding to UTF-8 and it should work. – Paresh Masani Sep 24 '12 at 12:05
0
//Conevrt the string utf-8 format

[yourstringvariabel UTF8String];
Rushabh
  • 3,208
  • 5
  • 28
  • 51
0

Using NSISOLatinStringEncoding solved this issue.

Suraj Sukale
  • 1,778
  • 1
  • 12
  • 19
Saikiran Komirishetty
  • 6,525
  • 1
  • 29
  • 36