views:

146

answers:

1

Hi,

I've got an NSTextView, inside an NSScrollView, and I want to scroll it to exactly where it used to be after repopulated the NSTextView with new data. So far, I've tried variations on the code below, but it never gets it quite right.

What I need is a setDocumentVisibleRect method, but there isn't one.

NSRect oldVisibleRect = [[[self scrollView] contentView] documentVisibleRect];

[Code to repopulate data]

[[[self scrollView] contentView] scrollToPoint:newPoint];

Any ideas?

+2  A: 

You'll need to be more specific - what about this isn't "quite right"?

-[NSView scrollRectToVisible:]

Scrolls the receiver’s closest ancestor NSClipView object the minimum distance needed so a specified region of the receiver becomes visible in the clip view.

NSTextView is a subclass of NSView (though you should always consider whether a given view has flipped coordinates...), so you can use -scrollRectToVisible:

Also, unless your text view content changes are pretty much exactly the same as before, the old visible rect isn't going to be quite the same, so I wouldn't expect it to behave perfectly. If you're only applying attributes (like syntax highlighting) to the text, there'd be no need to do anything to the text view (the text storage or container) that should require a complete reload (and re-scroll). I guess it boils down to "what exactly are you trying to do?"

Joshua Nozzi
Hi, managed to solve the problem by making all changes directly to NSTextStorage, and never interacting with the NSTextView directly.
MT
Most changes involving AppKit standard attributes can cause glyph placement to change. Consider converting the font to bold, for example: This can cause the line to break earlier, which in turn can cause the paragraph to break later, which would shift all subsequent paragraphs downward.
Peter Hosey