@DTS Engineer hanks for your thorough answer. I'm testing the approach in my project, and while it mostly works, I've run into one issue.
Occasionally, after scrolling the text view, viewportLayoutController.viewportRange ends up being nil, which causes the app to crash since the property is force unwrapped. I know I could use guard let or if let to prevent the crash, but that would prevent me from continuing the layout process.
Do you know what might cause viewportRange of NSTextViewportLayoutController to be nil, and how I can ensure it always has a value?
EDIT: I think I've solved this. It seems to be simply due to a pending layout. Calling viewportLayoutController.layoutViewport() prior to accessing viewportRange cause it to have a value. Now, I'm unsure why there's a pending layout since alignViewportToDocumentEndLocationIfNeeded() is called from textViewportLayoutControllerDidLayout(_:) at which point a layout was just completed.
Topic:
UI Frameworks
SubTopic:
General
Tags: