这一准则已经生效多年了。现在,在iOS 16和layoutManager = textView.layoutManager上运行时,我会遇到崩溃。
TextKit 1兼容模式似乎变得活跃起来,但如果第一个单词不是拉丁语,它仍然会在某些textView上崩溃。
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
}这是控制台日志:
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这是回溯:

在这种情况下,对于如何修复它或如何使用TextKit 2,有什么建议吗?
发布于 2022-12-01 16:10:20
我还看到了TextKit 2的崩溃。我的堆栈跟踪看起来与您的非常相似,确切的冒犯行看起来相当无害。这让我认为,在我的layoutManager子类上第一次引用UITextView时,它就崩溃了。
TextKit 2似乎不像TextKit 1在UITextView中所做的那样,所以我现在只是显式地使用TextKit 1(通过IB中的Text Layout选项),但稍后会重新讨论这个问题。
https://stackoverflow.com/questions/73739033
复制相似问题