Volodymyr
Volodymyr

Reputation: 1205

iOS html text is not working properly in attributed string

I'm trying to make part of text inside the label - bold. Here is a html code I'm using:

<style>body{line-height: 0;}</style><font face=\"Calibri\"><b>Get moving!</b> (and Sync to see your points.)</font>

But it's making all text bold. The same thing if HTML is using <b></b> tags. It will make all text regular.
Seems that it's checking only beginning.

Can anyone help me with that?

I'm using NSAtributedString for showing HTML text. Want to admit that it's working fine in HTML online editors.

Upvotes: 2

Views: 1771

Answers (2)

BSB
BSB

Reputation: 277

enter image description hereTry this:

 NSString *description = @"<style>body{line-height: 0;}</style><font face=\"Calibri\"><b>Get moving!</b> (and Sync to see your points.)</font>";
    self.lblDescription.attributedText=[self getData:description];

//Convert HtmlString to string
-(NSAttributedString *)getData:(NSString *)str
{
    NSData *stringData = [str dataUsingEncoding:NSUTF8StringEncoding];

    NSDictionary *options = @{NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType};
    NSAttributedString *decodedString;
    decodedString = [[NSAttributedString alloc] initWithData:stringData
                                                     options:options
                                          documentAttributes:NULL
                                                       error:NULL];
    return decodedString;
}

Upvotes: 1

user3182143
user3182143

Reputation: 9599

I got the solution.I tried sample one for your question.I got and it works fine.

NSString *strHTML = @"<style>body{line-height: 0;}</style><font face=\"Calibri\"><b>Get moving!</b> (and Sync to see your points.)</font>";
NSAttributedString *attrStr = [[NSAttributedString alloc] initWithData:[strHTML dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil];
lblHTMLString.attributedText = attrStr;

The Output Screenshot is

enter image description here

Upvotes: 1

Related Questions