Re: How to stop NSScrollView from scrolling to top when horizontally resizing contained NSTextView?
Re: How to stop NSScrollView from scrolling to top when horizontally resizing contained NSTextView?
- Subject: Re: How to stop NSScrollView from scrolling to top when horizontally resizing contained NSTextView?
- From: Ross Carter <email@hidden>
- Date: Wed, 18 Nov 2009 00:22:32 -0500
> Fundamentally, I want to:
>
> 1. Display lines of text that don't wrap in my NSTextView.
> 2. Display a horizontal scrollbar.
> 3. Make sure that the width of the NSTextView is appropriately wide
> for that line of text so that:
> 3.1 The user can scroll the width of the longest line of text and no more.
> 3.2 The scrollbar thumb is representative of that width.
>
> The samples I found online to enable horizontal scrolling set the
> width of NSTextContainer and NSTextView to the FLT_MAX, which is not
> what I want to do as that invalidates my 3rd requirement (above).
Sorry, I meant to reply to the list instead of directly to you. I think you will find that you still want the text container width to be something like FLT_MAX. You don't need to make the text container the same width as the longest line of text.
> Perhaps I'm misunderstanding, but this seems to be exactly how Xcode's
> text editor window works.
You mean when wrapping is turned off? You get that behavior for free. Try this:
1. Create a new project in XCode.
2. In the app delegate header, set up an IBOutlet to a NSTextView.
3. In interface Builder, add a NSTextView to the window and connect it to the IBOutlet.
4. Add this to the app delegate .m :
- (void)awakeFromNib {
[myTextView setHorizontallyResizable:YES];
NSSize tcSize = [[myTextView textContainer] containerSize];
tcSize.width = FLT_MAX;
[[myTextView textContainer] setContainerSize:tcSize];
[[myTextView textContainer] setWidthTracksTextView:NO];
}
Run the app. As you add text to the text view, you will see that the horizontal scroller adjusts as needed to accommodate the longest line. You don't have to do anything more.
From the docs: "Whether it tracks the size of its NSTextView or not, an NSTextContainer doesn’t grow or shrink as text is added or deleted; instead, the NSLayoutManager resizes the NSTextView based on the portion of the NSTextContainer actually filled with text." http://developer.apple.com/mac/library/DOCUMENTATION/Cocoa/Conceptual/TextStorageLayer/Tasks/TrackingSize.html#//apple_ref/doc/uid/20000927
-Ross_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden