0

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.

Jean
  • 7,623
  • 6
  • 43
  • 58
subharb
  • 3,374
  • 8
  • 41
  • 72
  • Escapes like `\u00f3` in string literals are evaluated at compile-time. If you have runtime data that has these escapes, they will not be interpreted. And `‘` isn't even a string escape; it's an HTML entity. – Lily Ballard Mar 25 '13 at 21:52

1 Answers1

3

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.

Community
  • 1
  • 1
Jean
  • 7,623
  • 6
  • 43
  • 58
  • 1
    I'm not questioning the validity of `-stringByDecodingHTMLEntities`, but for `-stringByConvertingHTMLToPlainText`, can't you just create an `NSAttributedString` from HTML and take its string value instead? – zneak Mar 25 '13 at 21:57
  • @zneak I was just quoting the author of the category, listing the available methods. But, yes, NSAttributedString has a great method `initWithHTML: dataUsingEncoding:` that can be used to build an attributed string from HTML code. The entities will indeed be interpreted, as well as the markup (that's why I didn't suggest it). Seems like the OP only wants to get rid of the html entities. – Jean Mar 25 '13 at 22:01
  • Could you explain more on that? I actully will need to parse any kind of HTML, I just realised that WS has some html tags. Thanks! – subharb Mar 26 '13 at 11:04
  • @DavidShaikh Done. I added more informations about **NSAttributedString initWithHTML: documentAttributes:]**. Please, note that there is a mistake in my comment above: it's not *`initWithHTML: dataUsingEncoding:`* but *`initWithHTML: documentAttributes:`* of course. – Jean Mar 26 '13 at 12:41
  • @DavidShaikh No, but there is an other way. I updated my answer accordingly. Hope this helps. – Jean Mar 26 '13 at 13:15