dar*_*iaa 5 html nsattributedstring ios ios7
我需要的是NSAttributedString从相对较大的HTML字符串创建对象并将它们(NSAttributedString-s)存储在数据库中.当然我想在后台线程中这样做.
这是一个简单的代码(失败)来演示我正在尝试做的事情:
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSString *HTMLString = @"<p>HTML string</p>";
NSDictionary *options = @{NSDocumentTypeDocumentAttribute : NSHTMLTextDocumentType, NSCharacterEncodingDocumentAttribute : @(NSUTF8StringEncoding)};
NSError *error = nil;
NSAttributedString *attributedText = [[NSAttributedString alloc] initWithData:[HTMLString dataUsingEncoding:NSUTF8StringEncoding] options:options documentAttributes:nil error:&error];
});
Run Code Online (Sandbox Code Playgroud)
根据这个讨论,可能根本不可能在后台线程中执行它,因为NSAttributedString使用NSHTMLTextDocumentType选项创建WebKit需要在执行任何异步工作时旋转runloop.
但也许有人想通了这条路?我有非常简单的HTML,只是文本和链接,可能有一种方法来指定NSAttributedString从HTML 创建一个不需要任何" WebKit-rendering"?
或者可能有人可以推荐第三方lib NSAttributedString从不使用的简单HTML 创建s WebKit?
您可以尝试NSAttributedString+DDHTML。AFAIK 它不依赖于 WebKit,我成功地将它用于简单的 HTML。我还没有尝试在后台线程中使用它,但我想这应该不是问题。
请注意 - 当 HTML 包含 iOS 上不可用的字体时,它可能会崩溃。我已经为此发出了拉取请求。
| 归档时间: |
|
| 查看次数: |
5348 次 |
| 最近记录: |