Unable to add exclusionPaths to "Meet TextKit 2" sample code

If I set an exclusion path, e.g. in TextDocumentViewController.viewDidLoad() like:


textLayoutManager.textContainer?.exclusionPaths = [bezierPath]

I am getting a "Unexpectedly found nil while unwrapping an Optional value" crash in TextDocumentView.adjustViewportOffset() because viewportRange is nil on textViewportLayoutController.

Setting an exclusion path also causes weird behavior for textLayoutManager.enumerateTextLayoutFragments() which refuses to iterate if the .ensuresLayout option is set.

The problem happens in both iOS and macOS (though I only care about iOS). I'm able to get exclusionPaths to work fine if I use UITextView, but the performance is unworkable there.

I also was able to set exclusionPaths in the STTextView project in GitHub, but I was unable to identify anything specific that code was doing differently.

Anyone have ideas as to what else needs to happen to make exclusionPaths work?

Replies

Following up on my own post:

If NSTextContainer height is set to 0 and exclusionPaths are set to nil, viewportRange seems to be nil as a result and enumerateTextLayoutFragments is unhappy. This feels like an Apple bug (I'm targeting/running on iOS16)? Anyone worked with exclusionPaths + TextKit successfully here?

Setting the NSTextContainer height to non-zero opens up a different can of worms even without exclusionPaths. Setting the height to anything but the content height leads to broken behavior after scrolling past the first page. Setting it to content height works until the amount of data gets large at which point it tanks (which it does not do if set to 0)

Reading the documentation makes it seem like the bounds of NSTextContainer should refer to visible bounds and not content bounds? Am I missing anything?