[TextKit2] - Wrong document height sometimes ?

Hello 👋

I'm playing with the Apple TextKit2 sample app (particularly on macOS).

I found that on some long document the evaluated height given by enumerateTextLayoutFragments API is wrong (or at least not I expect) which imply I can no longer scroll in my document even if I have not reached the end of it (which is not what I expect as you can guess). I'm clearly missing a point here. I can reproduce it on the Apple sample app by only changing the text content: https://developer.apple.com/documentation/UIKit/using-textkit-2-to-interact-with-text

Using TextKit2, is it my responsability as developer to check that I've reached end of the scrollview whether not being at the end location of the document and call some specific TextKit2 API to invalidate estimation or something ?

Here is an updated version of the Apple sample app with another text content that show the issue. https://drive.google.com/file/d/1jtTD84oqGAG4_A9DfqFl_yHmbLKhF1e8/view?usp=sharing

Environment: Xcode 16.4 - macOS 15.6

If someone could help me with this, I would be extremely grateful. It puzzles me.

NB: I've observed that resizing the window a bit seems to force a new layout and make TextKit2 returns a more accurate height, ... until you reach the end of the document.

Answered by DTS Engineer in 852061022

Are you using the system-provided NSTextView, or are you writing your own custom text view using TextKit2? If NSTextView demonstrates the issue, it is unexpected, and hence worth a feedback report, because NSTextView is supposed to handle the scrolling for you.

For your own custom text view, you should be able to fix the issue by adjusting the viewport. This post provides a code snippet for that purpose. Please see if that helps.

The sample app you mentioned is intended to demonstrate a text rendering view with TextKit2. It is a bug that the app presents the issue, and so please file a feedback report, if you don't mind.

Best,
——
Ziqiao Chen
 Worldwide Developer Relations.

Accepted Answer

Are you using the system-provided NSTextView, or are you writing your own custom text view using TextKit2? If NSTextView demonstrates the issue, it is unexpected, and hence worth a feedback report, because NSTextView is supposed to handle the scrolling for you.

For your own custom text view, you should be able to fix the issue by adjusting the viewport. This post provides a code snippet for that purpose. Please see if that helps.

The sample app you mentioned is intended to demonstrate a text rendering view with TextKit2. It is a bug that the app presents the issue, and so please file a feedback report, if you don't mind.

Best,
——
Ziqiao Chen
 Worldwide Developer Relations.

"Are you using the system-provided NSTextView, or are you writing your own custom text view using TextKit2?"

I've tested both during my investigation:

  • Custom text view - inherits NSView & using TextKit2: I'm able to reproduce the issue mentioned above (Xcode 16.4 - macOS 15.6)
  • Custom text view - inherits NSTextView & using TextKit2: I'm able to reproduce the issue mentioned above (Xcode 16.4 - macOS 15.6)
  • System-provided NSTextView: OK (Xcode 16.4 - macOS 15.6)

It really seems to be because of what value is returned by :

enumerateTextLayoutFragments(from: documentRange.endLocation, options: [.reverse, .ensuresLayout]) { layoutFragment in ... layoutFragment.layoutFragmentFrame.maxY ... }

"For your own custom text view, you should be able to fix the issue by adjusting the viewport. This post provides a code snippet for that purpose. Please see if that helps."

Yes indeed, I read this post and I can make a "workaround" with (OR without adjusting the viewport by ensuring a layout at appropriate time during scroll). That's why I thought it is may be intended to be that way.

"The sample app you mentioned is intended to demonstrate a text rendering view with TextKit2. It is a bug that the app presents the issue, and so please file a feedback report, if you don't mind."

Will do.

Thanks for your quick feedback very much appreciated!

As promised, filled under FB19433976

[TextKit2] - Wrong document height sometimes ?
 
 
Q