FROM : Stuart Malin
DATE : Thu May 01 21:12:48 2008
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_removed> wrote:
> Message: 10
> Date: Thu, 1 May 2008 11:39:42 -0600
> From: Randall Meadows <<email_removed>>
> Subject: Programmatic "Size To Fit"
> To: cocoa-dev cocoa-dev <<email_removed>>
> Message-ID: <<email_removed>>
> 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
DATE : Thu May 01 21:12:48 2008
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_removed> wrote:
> Message: 10
> Date: Thu, 1 May 2008 11:39:42 -0600
> From: Randall Meadows <<email_removed>>
> Subject: Programmatic "Size To Fit"
> To: cocoa-dev cocoa-dev <<email_removed>>
> Message-ID: <<email_removed>>
> 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
| Related mails | Author | Date |
|---|---|---|
| Randall Meadows | May 1, 19:39 | |
| Martin | May 1, 19:45 | |
| John Stiles | May 1, 19:48 | |
| Randall Meadows | May 1, 19:55 | |
| Michael Watson | May 1, 19:58 | |
| Stéphane Sudre | May 1, 20:00 | |
| Rob Napier | May 1, 20:02 | |
| John Stiles | May 1, 20:02 | |
| RICKY SHARP | May 1, 20:53 | |
| Stuart Malin | May 1, 21:12 |






Cocoa mail archive

