Reputation: 3472
Im saving data from a WebService and some of the info has ASCII caracters like ‘
or #038;
At the begining I thought that setting a string with a text containing caracacters like those would work perfectly since there are other, like accents \u00f3
, that appear decoded in the UILabels but those 2 for example don't.
Why is this? Am I understanding correctly?
Thanks.
Upvotes: 0
Views: 1431
Reputation: 7673
Since you are saving data from a WebService, you can use this NSString category for HTML as described in this post: Objective-C: How to replace HTML entities?
(Html entities are stuff like: ‘
- More on wikipedia)
The methods available in the category are:
- (NSString *)stringByConvertingHTMLToPlainText;
- (NSString *)stringByDecodingHTMLEntities;
- (NSString *)stringByEncodingHTMLEntities;
- (NSString *)stringWithNewLinesAsBRs;
- (NSString *)stringByRemovingNewLinesAndWhitespace;
[NSAttributedString initWithHTML: documentAttributes:]
Initializes and returns a new NSAttributedString object from HTML contained in the given data object.
NSData* htmlData = [yourHTMLString dataUsingEncoding:NSUTF8StringEncoding]
NSAttributedString* theAttributedString = [[NSAttributedString alloc] initWithHTML:htmlData documentAttributes:NULL];
I use this all the time and it works perfectly. I even made a macro out of it:
#define html2AttributedString(htmlString) \
[[[NSAttributedString alloc] initWithHTML:[(htmlString) dataUsingEncoding:NSUTF8StringEncoding] \
documentAttributes:NULL] autorelease]
Usage:
NSAttributedString* attributedString = html2AttributedString(yourNSString);
And then, you can make macro for color, alignment, font, etc…
For iOS, you can check out this page in which they give you a replacement.
Our initWithHTML methods aim to perfectly match the output from the Mac version. This is possible to achieve for characters and I have unit tests in place that make certain this keeps being perfect. Regarding the attributes there are many things that have to be done slightly different on iOS to achieve the same look. I won’t bother you with the details there.
Upvotes: 3