Ios 将UITableViewCell标签设置为从HTML字符串生成
我正在swift上使用TableView,我想知道如何设置UITableViewCell标签以从HTML字符串生成 我知道如何使用它在单元格标签上显示文本:Ios 将UITableViewCell标签设置为从HTML字符串生成,ios,uitableview,swift,tableview,tableviewcell,Ios,Uitableview,Swift,Tableview,Tableviewcell,我正在swift上使用TableView,我想知道如何设置UITableViewCell标签以从HTML字符串生成 我知道如何使用它在单元格标签上显示文本: override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCellWithIdent
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as UITableViewCell
var articlesInSection = xmlParser.channelsArray[indexPath.section]["articles"] as [AnyObject]
//articleWebView.loadHTMLString(activeItem, baseURL: nil)
cell.textLabel?.text = articlesInSection[indexPath.row]["title"] as NSString
return cell
}
但我有一个HTML,我想展示它,而不是文本,类似于loadHTMLString函数
我该怎么做呢?您可以使用NSAttributedString来显示html中的文本,但我认为这在表视图单元格中不是非常有效的解决方案,因为它会导致滚动时的延迟和/或加载时的延迟
var attrStr = NSAttributedString(
data:yourHTMLString.dataUsingEncoding(NSUnicodeStringEncoding, allowLossyConversion: true)!,
options: [ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType],
documentAttributes: nil,
error: nil)
cell.textLabel?.attributedText = attrStr
您可以使用
AttributedString
Swift 5中的样本
do {
let attrStr = try NSAttributedString(data: htmlStr.data(using: .unicode, allowLossyConversion: true)!, options: [.documentType : NSAttributedString.DocumentType.html], documentAttributes: nil)
cell.textLabel?.attributedText = attrStr
} catch {
print(error)
}
谢谢它可以工作,我只需要测试应用程序,因为我没有原始的应用程序代码,所以滞后对meCan来说是可以的,我对节标题也这样做?是的,你可以对任何标签这样做