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 PROTECTED] wrote:

Message: 10
Date: Thu, 1 May 2008 11:39:42 -0600
From: Randall Meadows <[EMAIL PROTECTED]>
Subject: Programmatic "Size To Fit"
To: cocoa-dev cocoa-dev <cocoa-dev@lists.apple.com>
Message-ID: <[EMAIL PROTECTED]>
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 (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to [EMAIL PROTECTED]

Reply via email to