Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSAttributedString performance is worse under iOS 8

Under iOS 8 (and 8.1 beta) the performance of creating an NSAttributedString is much worse than 7 (2-3x). This is especially noticeable if you're using multiple instances on the same view, loading 4 different labels will cause a delay of over a second from when the user taps and the new view appears.

Unfortunately you can't even throw this into another thread, since it uses WebKit behind the scenes. I have submitted a bug to Apple, but I need ideas on workarounds or a better implementation approach.

In viewDidLoad:

self.labelOne.attributedText = [[NSAttributedString alloc] initWithData:[htmlString dataUsingEncoding:NSUTF8StringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType }
                                                     documentAttributes:nil
                                                                  error:&error];

Quick sample project: https://github.com/BenSS/AttributedStringTest

UPDATE:
iOS9 improves things again, so the speed isn't completely crippling the UI interaction. Unfortunately it's still not as fast as this was under iOS7. (test it yourself with the demo!)

like image 700
Ben Avatar asked Oct 09 '14 14:10

Ben


1 Answers

At WWDC this year (2014) I brought this behavior up with the TextKit team. Specifically, I was elaborating on how some of us need support for asynchronous HTML parsing. They were surprised that there is no public API for attributed string creation asynchronously.

Given that the core of some of our apps deal with rendering large amounts of HTML text, the system provided rendering is less than ideal even on iOS 7. So, I was told to file a bug; the more bugs that are filed about this issue the higher the likelihood of Apple addressing it.

My suggestion is to use DTCoreText, they provide exactly what we all need regarding this issue. The only other solution is to wait.

like image 166
Daniel Galasko Avatar answered Nov 15 '22 23:11

Daniel Galasko