Very slow HTML rendering in NSAttributedString

后端 未结 1 752
太阳男子
太阳男子 2020-12-31 09:58

I have UITableView with dynamic sizing cells that displays list of comments in HTML format and I faced with the problem that NSAttributedString renders HTML content extremel

相关标签:
1条回答
  • 2020-12-31 10:22

    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.

    0 讨论(0)
提交回复
热议问题