2016-09-21 28 views
2

我正在尝试制作标签内部文本的一部分 - 粗体。这里是我正在使用的html代码:iOS html文本在属性字符串中无法正常工作

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

但它使所有文本变为粗体。如果HTML使用<b></b>标签,那也是一样。它会使所有文本都有规律。
似乎它只是检查开始。

任何人都可以帮助我吗?

我使用NSAtributedString来显示HTML文本。要承认它在HTML在线编辑器中工作正常。

+0

检查我的答案弗拉基米尔 – user3182143

回答

1

enter image description here试试这个:

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

它不工作。它使所有文字变粗。 – Volodymyr

+0

已找到问题。这不是HTML。你的代码工作正常。 – Volodymyr

+0

@Volodymyr请检查截图。它运作良好。 – Bhakti

1

我得到了solution.I尝试一个样本为您question.I了,它工作正常。

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; 

输出截图是

enter image description here

+0

我已经找到了一个错误。它与HTML无关。 – Volodymyr

+0

以上我的回答是制作正文部分的粗体文本的完美之作。我先在html在线编辑器中进行了检查。之后,只有我尝试过了。现在它的功能非常出色。 – user3182143

+0

其实并不完美。原因是因为将HTML发布到NSAttributedString中会导致WebKit工作,这会导致UI的巨大延迟。写起来更容易,这就是我选择它的原因。 – Volodymyr

相关问题