I have a list that uses UILabel for each of its rows. If I try to display special characters such as å,ä,ö, it displays them as å ä ö How do I convert them into a UTF8 encoded NSString?
Asked
Active
Viewed 4,307 times
1
-
what is the source of your data ? – Mutix Feb 23 '12 at 16:23
-
I'm getting it from my server. It displays fine in a webview but not in a uilabel. – Suchi Feb 23 '12 at 16:26
-
How are you setting the string into the label? Depending on how you get the text into the NSString you may need to set the encoding... have you looked at that? Also, what does the output look like if you log 1) the original string 2) the text of the UILabel after setting it? https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/Reference/NSString.html – David Rönnqvist Feb 23 '12 at 16:37
-
I just do [secondLabel setText:str]; where str is Swedish letters (å,ä,ö) – Suchi Feb 23 '12 at 16:52
-
in the log also I see it as (å,ä,ö) but in the webview I can see the actual characters – Suchi Feb 23 '12 at 16:53
-
See this discussion: http://stackoverflow.com/questions/1105169/html-character-decoding-in-objective-c-cocoa-touch Perhaps something there will be helpful – James Thiele Feb 23 '12 at 17:22
-
@Suchi in my case "Åland Islands" is not proper display in country list. so please give me a solution for this.Thanks. – Ilesh P Sep 23 '14 at 11:21
1 Answers
4
The characters display correctly in a WebView because the HTML entities are correctly interpreted by it.
Maybe this handy NSString category can help you to display the text how you want in a UILabel:
https://github.com/mwaterfall/MWFeedParser/blob/master/Classes/NSString+HTML.m
Import both NSString+HTML.h and NSString+HTML.m files, then in your class use
#import "NSString+HTML.h"
and then you can use
NSString *decodedString = [encodedString stringByDecodingHTMLEntities];
EDIT :
You can also try HerbertHansen's solution on the apple dev boards which doesn't need a whole library

Mutix
- 4,196
- 1
- 27
- 39
-
-
I have edited my answer with something you can try that doesn't need an external lib. – Mutix Feb 23 '12 at 19:10
-
The library works just fine. The edit did not work for me. Any other ideas? – Suchi Feb 23 '12 at 22:08
-
Is there a specific reason why is the use of a library a problem for you ? – Mutix Feb 23 '12 at 22:23
-
Just because of the increase in size. If there is a simpler solution I would definitely go for that. The library is the worst case for me. – Suchi Feb 23 '12 at 23:04
-
edited the answer again with a solution based on NSXMLParser that might fit your needs better in that case – Mutix Feb 23 '12 at 23:49
-
@Mutix hi, in my case "Åland Islands" is not proper display in country list. so please give me a solution for this.Thanks. – Ilesh P Sep 23 '14 at 11:20