对于我的语法突出显示实现,我观察到 NSTextView
的更改使用-[<NSTextStorageDelegate> textStorageDidProcessEditing:]
.
- (void)textStorageDidProcessEditing:(NSNotification *)notification {
if (!self.languageGrammar) return;
NSTextStorage *textStorage = self.textView.textStorage;
NSRange glyphRange = [self.textView.layoutManager glyphRangeForBoundingRect:self.scrollView.documentVisibleRect
inTextContainer:self.textView.textContainer];
NSRange editedRange = [self.textView.layoutManager characterRangeForGlyphRange:glyphRange actualGlyphRange:NULL];
[textStorage removeAttribute:NSForegroundColorAttributeName range:editedRange];
// crash is the line above ^^^^
// color text ...
}
我想获取可见字符的范围。上面的代码一直有效,直到我按下 退格键,这导致它崩溃:
*** -[NSConcreteTextStorage attributesAtIndex:effectiveRange:]: Range or index out of bounds
如何获取可见字符的范围以便为它们着色?
最佳答案
检查范围是否超出整个字符串的边界,以及是否设置范围适合字符串的边界:
NSRange range = NSRangeFromString(string);
关于objective-c - 如何获取 -textStorageDidProcessEditing :? 内可见的字符范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8854688/