简体   繁体   中英

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.

在此处输入图片说明 Try 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;
}

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

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM