Changing NSTextStorage Attributes Invokes Scrolling View

I implemented basic syntax highlighting by setting the NSTextStorage delegate of my NSTextView and changing the text attributes in -textStorageDidProcessEditing .

The main 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 called when you enter the SPACE character, the location of the NSTextView surrounding NSScrollView starts to roll (the scroll knob moves to some random position nearby)

What causes this?

+6
objective-c cocoa macos
source share
5 answers

I finally learned from my observations that jumps occur not only when you press the spacebar, but also for other keys, such as backspace, and this happens exactly when they both occur.
- Included non-contiguous layout

- Any modification, even attributes, of the text preceding the visible area is made inside -textStorageDidProcessEditing:
This seems to be a bug in the unsurpassed layout! It would be nice if the expert could confirm.
It seems to have nothing to do with the -beginEditing and -endEditing .

+11
source share

Eric I do not know if you decide this. However, I had a similar problem, and I found that I had to disable the Continuous Link option in the XCode 4.x attribute inspector for NSTextView in case this solves the problem. The documentation for NSLayoutManager provides additional hints (in the Overview section): "Unbiased layout is an optional layout manager behavior new in Mac OS X v10.5 ...".

Here is the message

In my case, I experienced this behavior regardless of the use of delegate methods or intermediate methods called through notifications, and it happened only when the contents of the text store became larger than the encompassing text view, as a result of which the scroll was active and the โ€œclickโ€ of the text view at the top . After disabling the "jump" option was no longer observed. Hope it helps. Tom

+6
source share

Turns off the call to -beginEditing and -endEditing inside the -textStorageDidProcessEditing: function -textStorageDidProcessEditing: not very cool! Instead, I switched to NSTextView -didChangeText .

+2
source share

Why not just remove the -beginEditing and -endEditing ? I had the same problem and this solves the problem for me.

+2
source share

textView.layoutManager?.allowsNonContiguousLayout = false

solved my problem

0
source share

All Articles