Re: dynamic TextView/TextContainer resizing.
Re: dynamic TextView/TextContainer resizing.
- Subject: Re: dynamic TextView/TextContainer resizing.
- From: Troy Stephens <email@hidden>
- Date: Fri, 15 Jul 2005 13:02:51 -0700
What you're trying to do can be accomplished with no code if you
like, using the view autoresizing mechnism. Set the horizontal-size
and right-margin springs for the view on the left, and the horizontal-
size and left-margin springs for the view on the right, and the
window width will be split evenly between them as the window is resized.
Alternatively, you might consider grouping the two views into an
NSSplitView, which would allow the user to apportion space between
them however is convenient.
That said, some comments on programmatic view resizing, which can be
useful for more complicated layouts:
On Jul 15, 2005, at 8:49 AM, David Harper wrote:
In my application's main window, there is an NSView subclass which
is used for geometry drawing. I set this view to the window's
delegate so that I can maintain that the view fills the left half
of the window:
- (IBAction)windowDidResize:(NSNotification *)aNotification
That's one way to hook into window resizing. An alternative would be
to have some coordinator object subscribe to
NSViewFrameDidChangeNotification for the containing superview (e.g.
in this case the window's contentView), and perform your layout
then. Another would be to override -resizeWithOldSuperviewSize: in
your graphic view and text view classes, and enable the
autoresizesSubviews feature in their containing superview. (I'd tend
to prefer the notification approach, as it decouples information
about how particular instances of the views are laid out from their
class implementations.
{
NSRect displayRect = [[self window] frame];
displayRect.origin.x = 0;
displayRect.size.width /= 2;
What you probably want instead [[self superview] bounds], as it
defines the area in which you want to do your layout, in the
appropriate coordinate system. (A view's frame is specified in its
superview's bounds coordinates.)
[self setFrame :displayRect];
[self setBounds:displayRect];
You don't need to set the bounds, when you just want them to have the
same size as the frame.
Also, note that the frame you're specifying in displayRect may have a
nonzero origin (to offset it from its superview's origin), whereas
you probably don't want to carry that same offset into the view's
bounds origin.
[self setNeedsDisplay:YES];
}
I want my TextView to do the same, except with the right half of
the window. However, textViews have no apparent setFrame or
setBounds methods.
NSTextView is a descendant of NSView, so it inherits all of NSView's
functionality and API.
Just in case, I tried adding the following code before the
setNeedsDisplay call (where engText is the IBOutlet* to my textView
subclass) :
displayRect.origin.x += displayRect.size.width;
[engText setFrame :displayRect];
[engText setBounds:displayRect];
If you resize the window afterward (by dragging the grow box) does
the layout then appear correct?
When you change a view's frame, you need to mark the affected areas
of its superview (both vacated and newly occupied) as needing
display. In this case, a simple:
[[self superview] setNeedsDisplay:YES];
should do what you need.
Unfortunately, and expectedly, these calls had no effect.
Strangely, Xcode compiled the code fine.
Essentially, my question is whether there is a way to achieve what
I would like those last 3 lines of code to do. Thanks in advance!
As noted above, you can even do it with no code at all! :-)
--
Troy Stephens
Cocoa Frameworks
Apple Computer, Inc.
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden