user1864371
user1864371

Reputation: 25

Using autolayout to manage height of UILabel within a subview

Using Xcode 5, interface builder and developing for iOS 7.

Within my content view I have 2 additional sub views, one on top of another. Within the upper subview I have a UILabel. I would like for that UILabel to expand in height when the content exceeds the first line, but I can't seem to get the height increase of the UILabel to increase the height of the subview, thus pushing the bottom subview down the main content view.

Additionally, I would assume the content view would need some sort of a constraint that reflects the overall height of the two subviews?

Perhaps this the question has already been answered somewhere, but I've searched everywhere and can't seem to come up with a solution.

Any help would be hugely appreciated! Thanks in advance.

Upvotes: 1

Views: 707

Answers (1)

Rafał Sroka
Rafał Sroka

Reputation: 40038

There is a couple of steps that you have to do to achieve this using autolayout.

  1. Set layout constrains for the label.
  2. Set height constraint with low priority.
  3. Set numberOfLines to 0 to allow multiline text.
  4. Set preferredMaxLayoutWidth for the label.

The preferredMaxLayoutWidth is used by label to calculate its height.

This property affects the size of the label when layout constraints are applied to it. During layout, if the text extends beyond the width specified by this property, the additional text is flowed to one or more new lines, thereby increasing the height of the label.

Also, have a look here.

Upvotes: 2

Related Questions