UITextView iOS16 - access to layoutManager property of textview cause crash

The code has been working for serval years. now it crashes when running on iOS 16 and on the layoutManager = textView.layoutManager .

It seems the TextKit 1 compatibility mode became active, but it still crashes on some of the textView if the first word is Arabic.

static func didTapTerm(_ sender: UITapGestureRecognizer) -> String? {
  guard let senderView = sender.view, (senderView is UITextView) else {
    return nil
  }

  let textView = senderView as! UITextView,
  layoutManager = textView.layoutManager
   
  var location = sender.location(in: textView)
  location.x -= textView.textContainerInset.left
  location.y -= textView.textContainerInset.top
   
  // find the value
  // character index at tap location
  let textContainer = textView.textContainer,
  characterIndex = layoutManager.characterIndex(for: location, in: textContainer, fractionOfDistanceBetweenInsertionPoints: nil),
  textStorage = textView.textStorage
   
  guard characterIndex < textStorage.length else {
    return nil
  }
   
  var range = NSRange()
  if let phrase = textStorage.attribute(customAttributedStringKey,
                   at: characterIndex,
                   effectiveRange: &range) {
    if let ph = phrase as? String {
      // update UI ...
    }
    return phrase as? String
  }
  return nil
}

This is a console log:

2022-09-15 13:29:55.532693-0700 DictionaryApp[1101:95240] [Text] UITextView 0x1051cd600 is switching to TextKit 1 compatibility mode because its layoutManager was accessed. Break on void _UITextViewEnablingCompatibilityMode(UITextView *__strong, BOOL) to debug.
2022-09-15 13:29:55.543047-0700 DictionaryApp[1101:95240] !!! _NSGlyphTreeInvalidateGlyphsForCharacterRange invalid char range 1
2022-09-15 13:29:55.543166-0700 DictionaryApp[1101:95240] !!! _NSGlyphTreeInvalidateGlyphsForCharacterRange character count mismatch
2022-09-15 13:32:06.118905-0700 DictionaryApp[1101:95240] *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[NSBigMutableString _getBlockStart:end:contentsEnd:forRange:stopAtLineSeparators:]: Range {5, 99} out of bounds; string length 103'
*** First throw call stack:
(0x1ab8f2248 0x1a4cbfa68 0x1a5d7c484 0x1b5b42260 0x1b5b5710c 0x1b5b55a14 0x1b5b73a20 0x1b5b73924 0x1b5b44df4 0x1b5b43cf4 0x1b5b639c0 0x1b5b61b08 0x1b5b61778 0x1b5b6125c 0x1ae9adcc4 0x1adaa76d0 0x1ae9ada3c 0x1adb8bfa0 0x1025ce384 0x102474d9c 0x102475154 0x1adb35164 0x1adea2dfc 0x1adc1cb94 0x1adafd1dc 0x1adba9358 0x1ae42a1d8 0x1adb6dd18 0x1adb72674 0x1adb71944 0x1adb71000 0x1adbb8d64 0x1adec14dc 0x1ab9be22c 0x1ab9ca614 0x1ab94e51c 0x1ab963eb8 0x1ab9691e4 0x1e4789368 0x1ade18d88 0x1ade189ec 0x102661fb0 0x1c9c8d948)
libc++abi: terminating with uncaught exception of type NSException
*** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[NSBigMutableString _getBlockStart:end:contentsEnd:forRange:stopAtLineSeparators:]: Range {5, 99} out of bounds; string length 103'
terminating with uncaught exception of type NSException

This is the backtrace:

UITextView iOS16 - access to layoutManager property of textview cause crash
 
 
Q