niks290192
niks290192

Reputation: 704

AutoLayout to dynamically size UILabel Height and Width

I have one UILabel and One UIView contains other subviews side by side in Storyboard. The UIView should butt up against the right edge of the UILabel(trailing constraint of 1), but I also need the UIlabel (the one on the left) to set it's width equal to it's content size unless it hits a max width. Visually:

|Label text| |UIViewWithSubviews|

And I need the following constraints:

1) Label should resize width wise unless it hits a max size and height is also dynamic that means when text reaches the maximum width then the word wrap will break the sentence to another line. and UIview will become vertical to UIlabel.

2) UIView should always be butted up against the right edge of label one in vertical center.

How do I set this up in Storyboard?

Thanks In advance.

Upvotes: 12

Views: 24098

Answers (2)

Daniel Hall
Daniel Hall

Reputation: 13679

1) Set a width constraint on the UILabel that is "Less Than Or Equal" to the max width you want.

2) Set the number of lines for the UILabel in the storyboard to be 0. This enables the wrapping you are looking for. Don't set any height constraint at all.

3) Set up constraint to center the UIView vertically with the UILabel.

4) Your other existing constraints sound fine. Just make sure that the UILabel has a clear x and y position, for example by anchoring to the leading and top edges of the superview. And make sure the UIView similarly has sufficient constraints as needed in addition to the vertical centering and the leading edge alignment with the UILabel.

Upvotes: 8

Tien
Tien

Reputation: 2215

Is this what you need?

enter image description here

Here is how i did in Interface Builder.

enter image description here

Upvotes: 26

Related Questions