7

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!)

Ben
  • 1,117
  • 13
  • 21

1 Answers1

5

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.

Daniel Galasko
  • 23,617
  • 8
  • 77
  • 97
  • 1
    It's ridiculous that they missed the poor performance of this under 8 since it was fine under 7. Thanks for the DTCoreText suggestion, since that's a good stopgap for our needs too. – Ben Oct 09 '14 at 17:05
  • I know, I think the guys at Apple have been pushed incredibly hard for iOS 8 and they have released a ton of great APIs but unfortunately some things have been neglected. Given how much emphasis has been placed on text kit we can only hope this will be addressed with iOS 9 – Daniel Galasko Oct 09 '14 at 20:20
  • @DanielGalasko Any idea if it was? – Carlos P Sep 25 '15 at 08:32
  • @CarlosP Just edited my question with a short update on iOS9. (TL;DR - It's better, but not back to 7 level) – Ben Sep 25 '15 at 11:40