0

In TextKit Best Practices WWDC lecture, it is mentioned that custom attributes can slow down NSTextView drawing, and recommends stripping those attributes before sending the string for rendering. I'm using custom attributes which contain tagging data which doesn't get rendered at all.

NSTextStorage has a method to fix the attributes, but I don't want to remove them altogether. The documentation for Attribute Fixing is a bit vague, and perhaps doesn't even apply in my case.

Which method and which class, NSTextView, NSTextStorage or NSLayoutManager, I can override to control the NSAttributedString that gets sent for drawing?

I've read the docs to best of my knowledge, and already use custom glyphs etc. successfully, but can't wrap my head around this. In the WWDC keynote, it is skimmed over quickly like it's as simple and easy as anything.

Tritonal
  • 607
  • 4
  • 16
  • The way I understood the lecture is: `NSTextView` is optimized. If you don't use a text view and draw the strings yourself then you should strip the custom attributes before drawing. Are the attributes slowing down the text view? – Willeke Oct 04 '21 at 09:41
  • There is an example flowchart right at the beginning about stripping the attributes before setting glyphs. "And if you're drawing an attributed string with a lot of custom attributes, this could also be slowing down your string drawing because the text system needs to validate all of the attributes before rendering and so for best performance, you should strip out extra attributes before drawing and only pass in the ones that are needed to determine the visual appearance like font or like color." – Tritonal Oct 04 '21 at 13:34
  • Little off-topic: you will experience more performance win when you strip all animations manually from all Layers that don't need them then fiddling with NSAttributedString overhead unless you have a lot of changing formatting in one large String. – Ol Sen Oct 04 '21 at 13:47
  • Where does the lecture mention that custom attributes can slow down `NSTextView` drawing? Do you draw an attributed string? Can't you just strip the attributes before drawing? Are the attributes or your code slowing down the text view? – Willeke Oct 04 '21 at 15:39
  • The quote above is from 5:30 in the WWDC session. And yes, rendering an attributed string which contains multiple custom attributes, is visibly slower than rendering one without them, as told in the WWDC, too: **"the text system needs to validate all of the attributes before rendering"**. This is true especially with longer document. – Tritonal Oct 04 '21 at 17:05
  • Oh well, I was looking at this from a too elaborate an angle. It's enough to subclass `NSTextStorage` and serve text attributes excluding the custom ones via `attributesAtIndex`, and when custom ones are needed, return them using a custom method. – Tritonal Oct 04 '21 at 18:00

0 Answers0