2

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.

1
  • Check my answer Volodymyr Commented Sep 22, 2016 at 10:34

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

8 Comments

I already found where was a bug. It was not relevant to HTML.
Above my answer is the perfect one for making bold text of body part.I checked in html online editor first.After that only I tried.Now it works fabulously.
Actually not as perfect. The reason for it is because posting HTML into NSAttributedString causes to work WebKit, which causes huge delays for UI. It's just easier to write, that's why I choose it.
If you have been asked the question " html string for webkit or dispaly the html string in webkit",I would be the first person give the solution.But you asked to display the html string in label with your exact output.
You are telling that it's best solution. It's not the best, previous answer have the same one and I previously commented as it's not working for me.
|
1

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;
}

4 Comments

Problem already found. It was not in HTML. your code works fine.
@Volodymyr Please check screenshot. It's working well.
Don't copy and post the answer once I post my answer with screenshot.
I didn't copy paste any one's screenshot. I added that screenshot only as a reply to Volodymyr's comment.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.