由于 NSAttributedString,UITableView 滚动不流畅
UITableView scrolling not smooth due to NSAttributedString
我正在使用 NSAttributedString
将 html 字符串转换为 attributedString
。我已经转换了它,但是在单元格中 label
所以我在 cellForRowAtIndex
中写了下面的代码,当我应用这个代码 tableview 时滚动不平滑。如果我用简单的文本删除它,它会平滑滚动。
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>
您需要使用调度队列
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
}
}
这个 html2AttributedString
有什么作用?它是否在 cellForRowAtIndexPath
上即时将 HTML 转换为属性字符串?如果是,这实际上需要时间。我们能否通过在模型的另一个变量中缓存 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.
...
}
希望对您有所帮助!
编辑:
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()
...
}
我正在使用 NSAttributedString
将 html 字符串转换为 attributedString
。我已经转换了它,但是在单元格中 label
所以我在 cellForRowAtIndex
中写了下面的代码,当我应用这个代码 tableview 时滚动不平滑。如果我用简单的文本删除它,它会平滑滚动。
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>
您需要使用调度队列
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
}
}
这个 html2AttributedString
有什么作用?它是否在 cellForRowAtIndexPath
上即时将 HTML 转换为属性字符串?如果是,这实际上需要时间。我们能否通过在模型的另一个变量中缓存 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.
...
}
希望对您有所帮助!
编辑:
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()
...
}