简体   繁体   English

后台线程中来自 HTML 的 NSAttributedString

[英]NSAttributedString from HTML in the background thread

What I need is to create NSAttributedString objects from relatively big HTML strings and store them (NSAttributedString-s) in the database.我需要的是从相对较大的 HTML 字符串创建NSAttributedString对象并将它们(NSAttributedString-s)存储在数据库中。 And of course I would like to do that in the background thread.当然,我想在后台线程中这样做。

Here is a simple code (which fails) to demonstrate what I'm trying to do:这是一个简单的代码(失败)来演示我正在尝试做的事情:

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];
});

According to this discussion it might not be possible to do it in the background thread at all, since creating NSAttributedString with NSHTMLTextDocumentType option uses WebKit which requires spinning the runloop while any asynchronous work is performed.根据此讨论,可能根本无法在后台线程中执行此操作,因为使用NSHTMLTextDocumentType选项创建NSAttributedString使用WebKit ,这需要在执行任何异步工作时旋转 runloop。

But may be someone else figured out the way?但也许其他人想出了办法? I have pretty simple HTMLs, just text and links, may be there is a way to specify that creating a NSAttributedString from the HTML will not require any " WebKit -rendering"?我有非常简单的 HTML,只有文本和链接,可能有一种方法可以指定从 HTML 创建NSAttributedString不需要任何“ WebKit -rendering”?

Or may be someone can recommend a third-party lib to create NSAttributedString s from simple HTMLs that does not use WebKit ?或者可能有人可以推荐第三方库来NSAttributedString使用WebKit简单 HTML 创建NSAttributedString s?

Yes you can't it in a background thread only on main thread.是的,您不能仅在主线程上的后台线程中使用它。 Use Oliver Drobnik's NSAttributedString+HTML class.使用 Oliver Drobnik 的NSAttributedString+HTML类。

Code would be something like this -代码将是这样的 -

NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc] initWithHTML:<html> dataUsingEncoding:NSUTF8StringEncoding] documentAttributes:nil];

Link - https://github.com/InfiniteLoopDK/NSAttributedString-Additions-for-HTML/blob/master/Classes/NSAttributedString%2BHTML.m链接 - https://github.com/InfiniteLoopDK/NSAttributedString-Additions-for-HTML/blob/master/Classes/NSAttributedString%2BHTML.m

You can try NSAttributedString+DDHTML .你可以试试NSAttributedString+DDHTML AFAIK it doesn't depend on WebKit and I used it successfully for simple HTMLs. AFAIK 它不依赖于 WebKit,我成功地将它用于简单的 HTML。 I haven't tried using it in the background thread, but I suppose it shouldn't be a problem.我还没有尝试在后台线程中使用它,但我认为这应该不是问题。

And watch out - it may crash when HTML includes font which is unavailable on iOS.并注意 - 当 HTML 包含在 iOS 上不可用的字体时,它可能会崩溃。 I've issued a pull request for that.我已经为此发出了拉取请求

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM