Saving and Restoring NSTextView Positions

I want to save the current text and visible area of ​​an NSTextView and restore it. Using visibleRect and scrollRectToVisible: seems to produce inconsistent results. If I just do:

- (void)restorePosition { NSRect r = [self.textView visibleRect]; [self.textView.layoutManager replaceTextStorage: self.textView.textStorage]; [self.textView scrollRectToVisible: r]; } 

the view will remain in a position where the view is located less than 85 lines from the top, but the farther, the further I get. On 200 lines from above, it ends at 277, and 300 - at 408, etc.

Without replaceTextStorage, it works just as expected, but replacing text and finding the previous location is a whole point. Something about replacing text makes it compute the rectangle of the rectangle.

Obviously, I plan to get a new visibleRect and textStorage from a saved object in a real application, but this illustrates a problem with minimal code.

Any ideas?

+4
source share
1 answer

I had a similar problem with restoring scroll position. For me, the solution was to format the text view before layout before changing the visible rectangle:

 [textView.layoutManager ensureLayoutForTextContainer:textView.textContainer]; 
0
source

All Articles