Very slow HTML rendering in NSAttributedString

ε祈祈猫儿з 提交于 2019-11-30 09:11:22
Arjan

ABout the slow parsing of the HTML into a string: The first time you create an attributed string of HTML, iOS creates all sorts of extra threads needed to parse the string, among which JavascriptCore engine.

Before parsing the first NSAttributedString from HTML:

And immediately after:

So you can imagine it takes almost a second sometimes to start this all up. Subsequent calls are much faster. My workaround was to parse HTML in the application:didFinishingLaunchingWithOptions: function in the Appdelegate, so that I had all necessary frameworks in memory when needed (Objective-C):

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    NSMutableAttributedString *attrStringFromHtml = [[NSMutableAttributedString alloc]
                                                     initWithData: [@"<span>html enabled</span>" dataUsingEncoding:NSUnicodeStringEncoding
                                                                                allowLossyConversion:NO]
                                                     options:@{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType}
                                                     documentAttributes:nil error:nil];
    NSLog(@"%@",[attrStringFromHtml string]);

    return YES;
}

Also see this answer.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!