views:

63

answers:

1

I'd like to provide a few pixels of padding around an NSTextView inside of an NSScrollView. I've reviewed this post and using setTextContainerInset does the trick for the left, right, and bottom margins. But as soon as the text scrolls the top buffer is lost. Same thing with overriding textContainerOrigin. Another answer in that post states:

The way TextEdit does it (when in Wrap to Page mode) is to put the text view inside of a larger view, and set that larger view as the document view of the scroll view.

But if I do that (using, say an NSBox) the content no longer scrolls. Am I missing something regarding that particular trick, or are there any other techniques that folks could suggest?

+1  A: 

There's a step that seems to be missing from your quote. You'll need to make sure your new document view tracks the changes in the text view's frame and sizes itself to fit. You can turn on NSViewFrameDidChangeNotification on your text view with -setPostsFrameChangedNotifications:, then have your new document view listen for notifications from your text view..

Joshua Nozzi
Thanks for the quick response. But once I've received an NSViewFrameDidChangeNotification letting me know that the inner text view want to scroll, how do I tell the outer scroll view?
stdout
Use NSTextView's -scrollRangeToVisible: (inherited from NSText) and pass in the text view's -selectedRange (which can include a zero-length insertion point).
Joshua Nozzi
Thanks again for the continued responses, but if I use scrollRangeToVisible (on the innermost text view where my text actually is) nothing seems to happen. I'm using:r = NSMakeRange ([[textView string] length], 0);[textView scrollRangeToVisible: r];But I thinking I'm missing something more fundamental here. With an NSBox "in between" my scroll view and my text view, won't all the "linkage" between the two be broken? e.g. how will the ScrollView know when to make the vertical scroll bar appear? And when I use the scroller, how will the text view know that it needs to scroll? And so on.
stdout
Read my comment again.You want to pass the text view's *selected range*, not the a range encompassing the entire body of text. As to how the scroll view determines how to work, this is covered in detail in the conceptual documentation.
Joshua Nozzi
Thanks, I did miss that. But turns our my real problem was that I wasn't manually resizing the NSBox (like you also said). Once I did that the scroll bar works as it should. My big problem now is figuring out the correct size the NSBox should be. I figured NSBox sizetoFit: would do the trick, and sometimes that works for the first line scroll that occurs. Then it quickly becomes "off" with the bottom row of characters being half off the window and no buffer up top. If you have any suggestions here or can point me in the direction of some documentation I should read up on I'd appreciate it.
stdout