2017-05-02 63 views
1

我使用NSAttributedString將html字符串轉換爲attributedString。我已經轉換它,但label在單元格,所以我寫下面的代碼cellForRowAtIndex,當我應用此代碼tableview不滾動平滑。如果我用簡單的文字刪除它,它會順利滾動。由於NSAttributedString UITableView滾動不平滑

cell.lblDescription.setHTMLFromString(htmlText: model.strItineraryDescription) 

我必須轉換到html stringattributed 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> 
+0

回答是實現您的要求的完美方式。 @NeverHopeless不錯的一個! +1 – Shardul

+0

@Shardul,謝謝。 – NeverHopeless

回答

2

這說明什麼html2AttributedString呢?它是否將HTML轉換爲cellForRowAtIndexPath上的動態屬性字符串?如果是的話,這實際上是花時間。我們可以通過在模型中緩存HTML的等效屬性字符串來加快內存速度。所以這種重新加載的方式會在單元創建或重用期間獲取準備好的屬性字符串。

例如,僞碼:

class MyModel 
{ 
    var myHTML: String = "" 
    var myHTML2AttributedString: String = "" 
} 


class ModelMapping 
{ 
    ... 
    myModel.myHTML = responseFromJSON["htmlText"] 
    myModel.myHTML2AttributedString = customFuncToConvertHTMLToAttributed(myModel.myHTML) 
    ... 
} 

class ViewController 
{ 
    ... 
    cell.lblDescription.attributedText = myModel.myHTML2AttributedString // This one would be cached Attributed string equivalent of HTML string. 
    ... 
} 

希望幫助!

編輯:從NeverHopeless

class MyModel 
{ 
    var myAttributedHTML: NSMutableAttributedString = "" 
    var strItineraryDescription: String = "" 

    func prepareHTMLFromString() { 
     let modifiedFont = NSString(format:"<span style=\"font-family: '-apple-system', 'HelveticaNeue'; font-size: \(self.font!.pointSize)\">%@</span>" as NSString, self.strItineraryDescription) 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) 


     myAttributedHTML = attrStr.mutableCopy() 
    } 
} 

class MyViewController 
{ 
    ... 
    cell.lblDescription.attributedText = myModel.myAttributedHTML 
    ... 
} 

class ResponseHandler 
{ 
    ... 
    myModel.strItineraryDescription = responseFromServer["myHTML"] 
    myModel.prepareHTMLFromString() 
    ... 
} 
+0

請檢查編輯的問題@NaverHopeless –

+1

@VinodKumar,好的,我在這裏說的是不要在顯示單元格時準備好你的屬性字符串,而應該在同時獲得'model.strItineraryDescription ',並在顯示屏上調用'cell.lblDescription.yourPropertyInModelObjectThatContainsAttributedString'。得到它了 ? – NeverHopeless

+0

我也以這種方式做了,但沒有工作。請幫助我@NeverHopeless –

1

您需要使用調度隊列

DispatchQueue.global(qos: .background).async { 
    print("This is run on the background queue") 
    let strmy = model.strItineraryDescription.html2AttributedString    

    DispatchQueue.main.async { 
     cell.lblDescription.attributedText = strmy 

    } 
} 
+0

現在我的細胞高度沒有增加,我正在使用估計的身高。 @KKRocks 'tblView.estimatedRowHeight = 185.0 tblView.rowHeight = UITableViewAutomaticDimension' –

+0

您可以在主線程中編寫代碼並查看。 – iPeter