mat*_*son 4 ios tttattributedlabel swift
我试图在UILabel中显示格式正确的HTML文本并成功使用以下代码:
// Create the html body
var attributedHTMLBody = NSAttributedString(data: comment.bodyHTML.dataUsingEncoding(NSUnicodeStringEncoding, allowLossyConversion: false), options: [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType], documentAttributes: nil, error: nil)
// Create the string including the text style
var htmlString = String(format: "<div style='font-size: 16px; font-family: HelveticaNeue-Light;'>%@", attributedHTMLBody.string)
// Create the final text to display
var attributedHML = NSAttributedString(data: htmlString.dataUsingEncoding(NSUnicodeStringEncoding, allowLossyConversion: false), options: [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType], documentAttributes: nil, error: nil)
// Set the text
cell.commentLabel.attributedText = attributedHML
// Find the locations of any links
var mutableLinkArray = NSMutableArray()
var mutableRangeArray = NSMutableArray()
attributedHML.enumerateAttributesInRange(NSMakeRange(0, attributedHML.length), options: NSAttributedStringEnumerationOptions.LongestEffectiveRangeNotRequired, usingBlock: {attribute, range, stop in
// Get the attributes
var attributeDictionary = NSDictionary(dictionary: attribute)
if let link = attributeDictionary.objectForKey("NSLink") as? NSURL {
mutableLinkArray.addObject(link)
mutableRangeArray.addObject(range)
}
})
// Add links to the label
for var i = 0; i < mutableLinkArray.count; i++ {
cell.commentLabel.addLinkToURL(mutableLinkArray[i] as NSURL, withRange: mutableRangeArray[i] as NSRange)
}
// Set the labels delegate
cell.commentLabel.delegate = self
Run Code Online (Sandbox Code Playgroud)
代码还可以正确查找和查找文本中的链接,并允许用户使用TTTAtributedLabel委托按下它们.
然而,此代码运行速度非常慢,表格单元格不允许平滑滚动,而是完全停止,然后在创建单元格后跳转.
就像一个注释,我已经尝试评论枚举属性,看看这是否是问题,但这根本不会加速细胞的创建.
如何改进此代码,谢谢!
使用UITextView.
使用iOS8,实现动态单元高度非常简单.使用文本视图设置单元格,并在文本视图和单元格上的其他视图周围设置约束.确保在界面构建器中将内容压缩阻力和内容拥抱优先级设置为100%.您可以在代码中实现所有这些,但在界面构建器中更简单.
现在在代码中,当您的控制器加载时,将估计的单元格高度设置为您认为有些准确的估计值.表视图将根据内容显示具有正确高度的单元格.
| 归档时间: |
|
| 查看次数: |
7046 次 |
| 最近记录: |