Domanda

I have a label which is going to contain a big description. I want the label to continue growing on new lines. In the image, its the label which starts with event_venue..... enter image description here

The even_venue.. label has 3 constraints for now:

  1. Vertical space with eventt_title
  2. a horizantal space with the leading of the superview
  3. a width constraints which defines that the label width is always less than the superview.width.

What I want to acheive is to make the event_venue.width less than superview.width, but if it has more text, it should display in new lines. Is this possible using autolayout?

È stato utile?

Soluzione

This are possible steps which can create expandable UILabel

  1. Set layouts for UILabel fixing its position
  2. Set number of lines = 0
  3. Set content vertical compression resistance to 1000 (this will allow text to push the label)
  4. Since you want UILabel to expand you cannot give it fixed height constraint or its parent fixed height constraint. Sometimes depending upon condition giving height constraint is necessary to avoid error then you need to set its priority lower than vertical compression resistance

Altri suggerimenti

Yes, this totally is possible. I see answers here that are close to solution but not complete. Here is a solution which works with auto layout in Storyboard, no coding of sizeToFit or anything. Your modified steps would be:

  1. Vertical space with eventt_title
  2. A horizontal space with the leading of the superview
  3. A horizontal space with the trailing of the superview
  4. Set UILabel's Line Breaks as Word Wrap.
  5. Set UILabel's lines property as 0.

I have solved a similar problem. I had to make a label that had a variable amount of text. Here's what I did:

  • In the storyboard, place your label with the origin where you want it.
  • In the Attributes Inspector, "Label" section, set the Line Breaks = Word Wrap
  • Fill the label with random placeholder text to the maximum shape you want. For example, if you wanted to fill the whole width and have room for a maximum of three lines of text, you could do:
    abcdefghijklmnopqrstu
    abcdefghijklmnopqrstu
    abcdefghijklmnopqrstu

  • In the code, set the text of the label using setText:
    [self.myLabel setText:@"MyLabelText"];

This did it for me. Your situation may be a little different in that I wasn't changing the width of the superview and it sounds like you might be. But if the width constraint is set on the label then I would expect this to work in your case, too.

I had a similar question about label resizing, and the answer that I found that was useful to me is here: UILabel Auto Size Label to Fit Text. This is a good starting source for code on how to resize your label programmatically.

I would recommend that you also add a horizontal trailing auto layout constraint from the label to the edge of the superview. Do that and you can then get rid of your current width constraint.

AutoLayout facilitate you for orientation purpose. I don think it will give you automatic expansion. You have to define label with width and height completely, otherwise you will see dots at the end of label. So you may use UITextView expanding it all over the screen. And set textView.backgroundcolot = clearColor.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top