Replacing HTML entities
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;
Response to comments
[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.