Chandran
Chandran

Reputation: 59

Parsing HTML from string

I am getting a string like this <b><i>Hello there </i></b> but in HTML format. This string is supposed to be bold and italicised. How would i convert this into "Hello there" and store it in a string to be used later on. Would i need to do something like HTML parsing?

Upvotes: 0

Views: 41

Answers (1)

AdamT
AdamT

Reputation: 6485

To store it you'd just assign it to a variable like:

NSString *helloWorldString = @"<b><i>Hello there </i></b>";

Later on, when you want to use it in a UILabel you can do something like this:

NSError *error = nil;
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 500, 100)];
label.attributedText =
[[NSAttributedString alloc]
          initWithData: [helloWorldString dataUsingEncoding:NSUTF8StringEncoding]
               options: @{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType}
    documentAttributes: nil
                 error: &error];

Upvotes: 1

Related Questions