Tapping a TextKit 2 backed UITextView moves the caret to a random location

With the upcoming launch of Apple Intelligence and Writing Tools, we've been forced to migrate our text editing app to TextKit 2. As soon as we released the update, we immediately got complaints about incorrect selection behaviour, where the user would tap a word to begin editing, but the caret would be shown in an undefined location, often dozens of paragraphs below the selected content.

To reproduce:

  1. Create a UITextView backed by a standard TextKit 2 stack and a large amount of text (50,000+ words) - see sample project below
  2. Scroll quickly through the text view (at least 20% of the way down)
  3. Tap once to select a position in the document.

Expected: The caret appears at the location the user tapped, and UITextView.selectedRange is the range of the text at the location of the tap. This is the behaviour of TextKit 1 based UITextViews.

Actual: The caret is positioned at an undefined location (often completely off screen), and the selectedRange is different to the range at the location of the tap, often by several thousand. There is no pattern to the magnitude of the discrepancy.

This incorrect behaviour occurs consistently in the sample project on the simulator, but you may need to hide the keyboard by pulling down, then repeat steps 2-3 a few times. This happens on iPhone and iPad, and on iOS 17, 18, and 18.1.

Do you have any insight into why this might be happening or how to work around this issue?

Sample code is here: https://github.com/nathantesler/textkit2-issue/tree/master

I see that a UITextView bug and would suggest that you file a feedback report – If you do so, please share your report ID here for folks to track.

Best,
——
Ziqiao Chen
 Worldwide Developer Relations.

Thanks. Filed as FB15241823

Tapping a TextKit 2 backed UITextView moves the caret to a random location
 
 
Q