Re: Programmatic "Size To Fit"
Re: Programmatic "Size To Fit"
- Subject: Re: Programmatic "Size To Fit"
- From: Stuart Malin <email@hidden>
- Date: Thu, 1 May 2008 09:12:48 -1000
SizeToFit will expand horizontally. If you need to constrain
horizontal width... here's some code I use resize NSTextField
instances vertically while constraining the horizontal width to fit
within some contained view. I believe this approach would work with
other sorts of controls. Adapt to your needs...
float targetWidth = <<code snipped -- derive a value>>; // subtract
4 for 2 pixel gutter on either side
// resize the textField to make all the text visible
NSRect r = NSMakeRect(0, 0, targetWidth, MAXFLOAT); // confining
bounds: limited width, huge height
NSSize s = [[textfield cell] cellSizeForBounds:r]; // returns
minimum size needed to hold the receiver
s.width = targetWidth; // force to consistent width
[textfield setFrameSize:s]; // and now set the textField's frame
size
In my application, the textfield sits within some other view
(theContentView), and I get its width thus:
float targetWidth = [theContentView frame].size.width - 4.0; //
subtract 4 for 2 pixel gutter on either side
If you have an existing control and want its width to remain fixed,
you could always get its current width and use this as the targetWidth.
HTH,
--Stuart
On May 1, 2008, at 7:58 AM, email@hidden wrote:
Message: 10
Date: Thu, 1 May 2008 11:39:42 -0600
From: Randall Meadows <email@hidden>
Subject: Programmatic "Size To Fit"
To: cocoa-dev cocoa-dev <email@hidden>
Message-ID: <email@hidden>
Content-Type: text/plain; charset=US-ASCII; format=flowed; delsp=yes
I am creating a bunch of controls (at least NSTextfield,
NSPopupButton, NSSlider, and perhaps others) programmatically (that
will eventually be shown in an NSTableView), and would like to apply
the "Size To Fit" feature that IB provides. However, there doesn't
seem to be any API that does that.
Does IB simply brute-force the resizing for every type of view, or am
I missing the appropriate call in the NSView API?
Thanks!
randy
_______________________________________________
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