Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
735 views
in Technique[技术] by (71.8m points)

objective c - Modifying NSTextStorage attributes causes scroll view to jump around

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 NSTextViews 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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I have finally found out from my observations that the jumping happens not only when pressing spacebar but for other keys such as backspace as well and this happens exactly when both of these happen.
- Non-contiguous layout is turned on
- Any modification, even to attributes, of text preceding the visible region is made inside -textStorageDidProcessEditing:
Looks like it is a bug in Non-contiguous layout feature! Would be good if an expert could confirm.
It appears to have nothing to do with calling -beginEditing and -endEditing.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...