Objective c 如何从-textStorageDidProcessEditing:中获取可见字符的范围?
对于语法突出显示实现,我使用Objective c 如何从-textStorageDidProcessEditing:中获取可见字符的范围?,objective-c,cocoa,syntax-highlighting,appkit,nslayoutmanager,Objective C,Cocoa,Syntax Highlighting,Appkit,Nslayoutmanager,对于语法突出显示实现,我使用-[textStorageDidProcessEditing:][/code>观察对NSTextView的更改 - (void)textStorageDidProcessEditing:(NSNotification *)notification { if (!self.languageGrammar) return; NSTextStorage *textStorage = self.textView.textStorage; NSRange glyph
-[textStorageDidProcessEditing:][/code>观察对NSTextView
的更改
- (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 ...
}
我想得到可见字符的范围。上面的代码一直工作到我点击backspace,这使它崩溃:
*** -[NSConcreteTextStorage attributesAtIndex:effectiveRange:]: Range or index out of bounds
如何获取可见字符的范围,以便为其着色?检查范围是否超出整个字符串的边界,以及是否将范围设置为适合字符串的边界:
NSRange range = NSRangeFromString(string);
崩溃时,editedRange
的值是多少?