With TextKit 1, I was able to “tag” characters with attribute string keys that flagged them to be invisible, then I would use NSLayoutManager’s layoutManager(_:shouldGenerateGlyphs:properties:characterIndexes:font:forGlyphRange:) to strip these characters out, preventing change to the underlying storage.
In TextKit 2, I don’t see an opportunity to do this. The best point I think to intercept would be NSTextLayoutFragment, but without being able to see what’s happening, I don’t know if it’s possible to alter the content used to generate the line fragments.
My end goal is to be able to hide characters for a Markdown editor, so maybe I’m thinking about this wrong? Any advice would be welcome.
TextKit 2 doesn't go into glyphs, but when rendering text, it calls textLayoutManager(_:textLayoutFragmentFor:in:) to give the delegate an opportunity to return a custom text layout fragment for a text element (or a paragraph), with which you can draw the text element in your own way. Based on this, I believe the high level flow to achieve your goal will be the following:
- Maintain a list of the ranges of the text that should be hidden.
- Implement
textLayoutManager(_:textLayoutFragmentFor:in:)
to return a customNSTextLayoutFragment
object, if thetextElement
argument contains hidden text. - Draw your custom
NSTextLayoutFragment
in a way that ignores the hidden text. - Manage the cursor and selection so the hidden text is ignored as well when editing.
If you go along this path and see anything that doen't work, I'd be curious to take a closer look.
Best,
——
Ziqiao Chen
Worldwide Developer Relations.