1

Here is the screenshot of UIView with its constraints.

It is located in a tableview cell

enter image description here

When I use UIlabel without UIView I can get it to grow multiline. But It doesn't work with UIView which I use for padding

enter image description here

I tried sth like

cell.layoutIfNeeded()
        cell.label.sizeToFit()
        cell.label.layoutIfNeeded()
        cell.view.layoutIfNeeded()
        cell.view.sizeToFit()

How can I make multiline work?

Utku Dalmaz
  • 9,780
  • 28
  • 90
  • 130

3 Answers3

1
  • Make sure you did not set any required static height for the label nor the view itself.
  • Make the label (which is the subview of the view) edges equal to the superview's.
  • Last but not least, set the values of content hugging and content compression resistance priorities to those you need.

And btw. - If all you need is the grey background that visually wraps the label by some constant, the label doesn't have to be a subview of that grey background view, just set the view's edges to the label's ones and do some layout corrections to fit your needs.

Jakub Truhlář
  • 20,070
  • 9
  • 74
  • 84
0

1.Set background color from attribute inspector for Label.Don't use background view for label.

2.Set UIlabel's lines property to no of lines that you want.

3.Don't give height constraints.give only top leading trailing and width.

Sagar koyani
  • 403
  • 2
  • 12
0

1- Set UIlabel's lines property to no of lines that you want, you can set it to 0, then the UILabel will automatically resize

2- Don't give height constraints, give only top leading trailing and width

3-return in heightForRowAtIndexPath: UITableView.automaticDimension (Swift 4.2)

Roy
  • 1
  • 1