Re: Force NSTextField (multiline wrapping label) to grow vertically?
Re: Force NSTextField (multiline wrapping label) to grow vertically?
- Subject: Re: Force NSTextField (multiline wrapping label) to grow vertically?
- From: Ken Thomases <email@hidden>
- Date: Thu, 09 Apr 2015 14:08:59 -0500
On Apr 9, 2015, at 1:53 PM, Steve Mills <email@hidden> wrote:
> On Apr 9, 2015, at 13:28:33, Ken Thomases <email@hidden> wrote:
>>
>> In a parent view, you could override the -layout method with something like this to adjust it dynamically:
>>
>> - (void) layout
>> {
>> textField.preferredMaxLayoutWidth = 0;
>> [super layout]
>> textField.preferredMaxLayoutWidth = NSWidth([textField alignmentRectForFrame:textField.frame]);
>> [super layout];
>> }
>
> Groovy! That works like a charm. I was curious, so I commented out the first 2 lines, and it still works as expected. Do you think the reset to 0 and subsequent forced layout are necessary? Thanks again, Ken.
The first line may or may not be necessary depending on the other constraints in your window. The problem it solves is that the last preferredMaxLayoutWidth may prevent the text field from growing any wider, even if you've increased the width of the window. For example, if you have "[textField]-(>=20)-|", then shrinking the window will force the text field to shrink, but growing the window won't _force_ the text field to grow. It will _allow_ it to, but it might still not if its intrinsic width (from the last preferredMaxLayoutWidth) is narrower and nothing forces it. So, in this case, preferredMaxLayoutWidth will only ever be made smaller, never larger.
On the other hand, if you have "[textField]-|" or the like, and the text field's horizontal content hugging priority is lower than 500, then growing the window will stretch the text field beyond its intrinsic size and that will allow the following lines to compute the appropriate preferredMaxLayoutWidth so that the text field will compute an appropriate intrinsic height (and new intrinsic width). In this case, the first line is not necessary.
The second line is necessary in order to be sure that the text field's frame has been updated before you use it. Without it, you can't be sure that the frame has any meaningful value in the third line.
Regards,
Ken
_______________________________________________
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