如何获取从 -textStorageDidProcessEditing: 中可见的字符范围? [英] How to get the range of characters that are visible from within -textStorageDidProcessEditing:?
本文介绍了如何获取从 -textStorageDidProcessEditing: 中可见的字符范围?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
对于我的语法高亮实现,我使用 -[<NSTextStorageDelegate> 观察了
.NSTextView
的变化.textStorageDidProcessEditing:]
For my syntax highlighting implementation, I observe changes to an NSTextView
using -[<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 ...
}
我想获取可见字符的范围.上面的代码一直有效,直到我点击 backspace,这让它崩溃了:
I want to get the range of visible characters. The above code works until I hit backspace, which makes it crash:
*** -[NSConcreteTextStorage attributesAtIndex:effectiveRange:]: Range or index out of bounds
如何获取可见字符的范围以便为它们着色?
How would I get the range of visible characters so I can color them?
推荐答案
检查范围是否超出了整个字符串的边界,如果设置的范围适合字符串的边界:
Check if the range is beyond the bounds of the entire string, and if it is set the range to fit the bounds of the string:
NSRange range = NSRangeFromString(string);
这篇关于如何获取从 -textStorageDidProcessEditing: 中可见的字符范围?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文