在界面布局中,我們會用到各種控件來顯示我們想要的數據來進行展示;之前遇到過這樣一個問題,我從后台的數據傳過來是json,解析過后直接放入一個TextView來進行顯示,但是沒注意到這個數據里面是HTML格式的,直接用了

結果出現了下面的情況:

未處理.png
查了一些資料,發現在TextView中有個attributedText屬性,在Label中也有,具體的就不在這里說了,可以自行查詢。來說說怎么解決這個問題。
如下圖:

第二個方法.png
這個就是把數據的Model先獲取,然后轉換過來。然后就OK了:

處理后.png
NSAttributedString *attributedString = [[NSAttributedString alloc] initWithData:[htmlString dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil]; self.msg.attributedText = attributedString;