I have implemented basic syntax highlighting by properly setting the NSTextStorage
delegate of my NSTextView
and changing the text attributes in -textStorageDidProcessEditing
.
The basic process is as follows
- (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];
}
Whenever -removeAttribute:range:
or -addAttribute:value:range
is invoked when a SPACE
character is entered, the NSTextView
s surrounding NSScrollView
location begins to jump around (scroll knob goes to some random position near the )
What's causing this?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…