2017-05-02 65 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