gklka
gklka

Reputation: 2574

How to make UILabel to fill available space with autolayout?

I have a text label which can contain text in length from a few words to a few paragraphs. I want it to behave something like a <p> does in HTML. It is set up like this:

Interface designer set up

It is attached to the left ("Equals 18 pixels"), to the top ("Equals 31 pixels") and to the right ("Greater than or Equal").

The result looks like this:

enter image description here

You can see, that autolayout is right, all the constraints are fulfilled, but it is not what I wanted. It somehow misses an equalation which tells the value label to fill the maximum space horizontally it can fill. How can I set this up using Storyboards?

Upvotes: 4

Views: 6510

Answers (2)

Rick Pastoor
Rick Pastoor

Reputation: 3635

This should to the trick:

  1. Remove the right constraint.
  2. Drag the UILabel to the desired width.
  3. Add a new constraint by selecting the UILabel and use the Editor > Pin > Trailing Space to Superview menu option.

Upvotes: 4

Mahesh
Mahesh

Reputation: 524

Select your UILabel and then in Editor menu select Size to Fit content Also remove the right constraint ("Greater than or Equal").

Doing this, your label will stretch horizontally to fit its content. This will solve your problem.. :)

Upvotes: 0

Related Questions