简体   繁体   中英

Multiline problem of UILabel embed in UIView

Here is the screenshot of UIView with its constraints.

It is located in a tableview cell

在此处输入图片说明

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

在此处输入图片说明

I tried sth like

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

How can I make multiline work?

  • 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.

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.

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM