2016-09-21 30 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

相關問題