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 13:28:33 -0500
On Apr 9, 2015, at 11:24 AM, Steve Mills <email@hidden> wrote:
> I can't figure out a combination of constraints and hugging/compression values that will cause an NSTextField used as a multiline wrapping label to grow vertically when the window is resized horizontally.
> I tried setting the label's vertical content hugging to 1 and the vertical content compression resistance to 1000, which seem like the right values to me. Does its intrinsic size never grow to fully contain the wrapped text?
Constraints never affect a view's intrinsic content size, at least not without some help from you. The content hugging and compression resistance priorities take the intrinsic content size as input, more or less. Or, thought of another way, they act based on the intrinsic content size.
As you've found, by default, a text field will compute an intrinsic content size as though it were laid out in one long line.
Since 10.8, though, NSTextField has had a preferredMaxLayoutWidth property. If you set that to a non-zero value, it will compute its intrinsic content size based on the rect it would need if it were wrapped at that width.
That's still static, though. By itself, it doesn't help as your window changes width.
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];
}
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