最近我正在尝试实现一个富 TextView 来显示主题内容或回复等内容(通常包括一些图像)。我在UITableView中尝试了DTCoreText,但似乎有很多问题。
我刚刚发现从iOS 7开始,UITextView/UILabel开始支持NSAttributedText,但问题是所有图像都是同步加载的,它们会阻塞主线程。有什么办法可以异步加载它们吗?
最佳答案
NSAttributedString *attributedString = [[NSAttributedString alloc] initWithData:[htmlString dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil];
在这一行中,您应该先下载数据,然后再将其设置为属性字符串。
dispatch_async(dispatch_get_global_queue(0, 0), ^{
// Load Data Here.
dispatch_async(dispatch_get_main_queue(), ^{
// Create NSAttributedString and set data to it.
});
});
关于ios - 可以异步加载 UILabel/UITextView 的 NSAttributesString(从 HTML 生成)中的图像(iOS 7+)吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28984783/