我使用NSAttributedString
將html字符串轉換爲attributedString
。我已經轉換它,但label
在單元格,所以我寫下面的代碼cellForRowAtIndex
,當我應用此代碼tableview不滾動平滑。如果我用簡單的文字刪除它,它會順利滾動。由於NSAttributedString UITableView滾動不平滑
cell.lblDescription.setHTMLFromString(htmlText: model.strItineraryDescription)
我必須轉換到html string
attributed string
extension UILabel {
func setHTMLFromString(htmlText: String) {
let modifiedFont = NSString(format:"<span style=\"font-family: '-apple-system', 'HelveticaNeue'; font-size: \(self.font!.pointSize)\">%@</span>" as NSString, htmlText) as String
//process collection values
let attrStr = try! NSAttributedString(
data: modifiedFont.data(using: .unicode, allowLossyConversion: true)!,
options: [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType, NSCharacterEncodingDocumentAttribute: String.Encoding.utf8.rawValue],
documentAttributes: nil)
self.attributedText = attrStr
}
}
我html string
是
<strong><u>Featured Program</u></strong></p>
\n<ol>
\n <li>Arrival at Delhi airport. You will receive by our representative.</li>
\n <li>Driving to Agra and exploring the city.</li>
\n <li>Back to Hotel for Overnight stay.</li>
\n</ol>
\n<p><strong>City Features</strong></p>
\n<ol>
\n <li><strong>Visit to Agra Fort \U2013 </strong>Former Name Badalgarh, Shah-Jahan spent his last eight years here. You can watch the mesmerizing view of Taj Mahal from this fort just like Shah Jahan.</li>
\n <li><strong>Visit to Taj Mahal \U2013</strong> It took 21 years to build and stood in 1653. The palace is considered as the symbol of love. Shah Jahan built this wonder in the memory of his wife Mumtaj Mahal.</li>
\n</ol>
\n<p><strong>(Both Agra Fort and Taj Mahal are UNESCO Declared world heritage site)</strong>
回答是實現您的要求的完美方式。 @NeverHopeless不錯的一個! +1 – Shardul
@Shardul,謝謝。 – NeverHopeless