Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change uilabel height based on content in a tableview cell

I want to expand label's height based on the text inside. I use tableviewcontroller with static cells.

enter image description here

I have an UILabel in a tableview cell and I set top, bottom, right and left constraints of the label.

I tried these solutions:

self.eDesc.numberOfLines = 0
self.eDesc.lineBreakMode = NSLineBreakMode.byWordWrapping
self.eDesc.sizeToFit()

self.tableView.estimatedRowHeight = 140
self.tableView.rowHeight = UITableViewAutomaticDimension

But none of them worked.

Is there any other approach on this (maybe changing size by label frame)?

like image 435
Utku Dalmaz Avatar asked Jan 15 '18 13:01

Utku Dalmaz


1 Answers

To set automatic dimension for row height & estimated row height, ensure following steps to make, auto dimension effective for cell/row height layout. I just tested following steps and code and works fine.

  • Assign and implement tableview dataSource and delegate
  • Assign UITableViewAutomaticDimension to rowHeight & estimatedRowHeight
  • Implement delegate/dataSource methods (i.e. heightForRowAt and return a value UITableViewAutomaticDimension to it)

-

@IBOutlet weak var table: UITableView!

override func viewDidLoad() {
    super.viewDidLoad()

    // Don't forget to set dataSource and delegate for table
    table.dataSource = self
    table.delegate = self

    // Set automatic dimensions for row height
    // Swift 4.2 onwards
    table.rowHeight = UITableView.automaticDimension
    table.estimatedRowHeight = UITableView.automaticDimension


    // Swift 4.1 and below
    table.rowHeight = UITableViewAutomaticDimension
    table.estimatedRowHeight = UITableViewAutomaticDimension

}



// UITableViewAutomaticDimension calculates height of label contents/text
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    // Swift 4.2 onwards
    return UITableView.automaticDimension

    // Swift 4.1 and below
    return UITableViewAutomaticDimension
}

For label instance in UITableviewCell

  • Set number of lines = 0 (& line break mode = truncate tail)
  • Set all constraints (top, bottom, right left) with respect to its superview/ cell container.
  • Optional: Set minimum height for label, if you want minimum vertical area covered by label, even if there is no data.

enter image description here

Edit: You must be missing top or bottom or height layout constraint for anyone UIElement in cell. Ensure all UIElements in cell, have valid top and bottom (and height, whereever required) constraints attached with it vertical (adjacent) UIElement.

Note: If you've more than one labels (UIElements) with dynamic length, which should be adjusted according to its content size: Adjust 'Content Hugging and Compression Resistance Priority` for labels which you want to expand/compress with higher priority.

Here in this example I set low hugging and high compression resistance priority, that leads to set more priority/importance for contents of second (yellow) label.

enter image description here

like image 143
Krunal Avatar answered Sep 23 '22 18:09

Krunal