我已经通过正确设置NSTextView
的NSTextStorage
委托和更改-textStorageDidProcessEditing
中的文本属性来实现基本语法高亮。修改NSTextStorage属性会导致滚动视图跳转
基本过程如下:
- (void)textStorageDidProcessEditing:(NSNotification *)notification {
NSTextStorage *storage = [notification object];
[storage beginEditing];
NSString *text = [storage string];
NSRange textRange = NSMakeRange(0, [text length]);
[storage removeAttribute:NSForegroundColorAttributeName range:textRange];
// Some regex matching here ...
[storage addAttribute:NSForegroundColorAttributeName
value:[COSyntax colorForPatternGroup:pattern.groupName]
range:capturedRanges[group]];
[storage endEditing];
}
每当输入SPACE
字符时调用-removeAttribute:range:
或-addAttribute:value:range
,围绕NSScrollView
位置的NSTextView
开始跳转(滚动旋钮到达附近的某个随机位置) )
造成这种情况的原因是什么?
我刚刚有这个问题,我非常感谢这个答案。拯救了我无尽的悲伤。 – 2012-06-21 22:20:53
我在'processEditing'中处理自定义文本存储。移动到委托方法解决了我的问题。谢谢! – 2013-12-31 04:10:35
这似乎是正确的答案,应该是被接受的答案。关闭IB中的非连续布局可以解决问题。其他建议的修正,例如在'textStorageDidProcessEditing:'内部使用'didChangeText',或不使用'beginEditing' /'endEditing',不是一个好主意,不管它们是否避免了这个特定的错误。我刚刚在苹果的bug记者提交了这个bug,#24539235。请提交bug! – bhaller 2016-02-06 13:41:27