Dynamically Increase Height of Uilabel & Tableview Cell

Dynamically increase height of UILabel & TableView Cell?

First of all you should not calculate height manually in auto layout environment. Just set both labels TopSpace and BottomSpace to cell's contentView and make sure you set both labels NumberOfLines to 0 and LineBreakMode to WordWrap.

And the other constraint are as below,

ItemLabel:

Sample Image

SeparatorView:

Sample Image

DescriptionLabel:

Sample Image

And add the delegates for height as below,

#pragma mark - UITableView Delegates
-(CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath {

return 44.0;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {

return UITableViewAutomaticDimension;
}

You should get the output as below,

Sample Image

Hope this would help you.

Changing UILabel text length in UITableViewCell with dynamic height does not resize it correctly

I have found a solution that works, after updating the UILabel content I am calling:

 UIView.animateWithDuration(0.3) {
cell.contentView.layoutIfNeeded()
}

To update cell constraints. And then to resize the cell height:

 tableView.beginUpdates()
tableView.endUpdates()

I will wait to accept this answer to see if someone else will answer a more elegant solution though.

Dynamically change cell height according to label text

The bottom anchor needs to be set:

cevapLabel.bottomAnchor.constraint(equalTo: bottomAnchor).isActive = true

Multiple UILabels in UITAbleViewCell with dynamic height

Add a (lower priority) fixed height constraint for the cell (e.g. = 40).

Add a top and bottom constraint for each label. Make the top constraint a fixed margin (e.g. = 8). Make the bottom constraint a (higher priority) greater than or equal constraint (e.g. >= 8).

The cell's height will be determined by the taller label, and all the other vertical constraints will still be met.

Update:

It turns out that you don't need a fixed height constraint for the cell, or even need to tweak the horizontal content hugging or compression resistance priorities.

I pinned the labels' top and left or right edge to the superview.margin, = 0, and added a horizontal space between the labels >= 4.

I pinned the labels' bottom edge to the superview.margin, >= 0. I also set both labels' vertical compression resistance priority to 1000.

At this point, we've set the minimum necessary constraints. However, if both labels are long, one label will take most of the width, forcing the other label to be extremely narrow. I arbitrarily added a width >= 100 constraint to both labels to balance things out.

Label constraints

Enable self-sizing in -viewDidLoad, then assign your labels' text, and have the cell lay itself out. It wasn't necessary to override anything in the custom UITableViewCell.

self.tableView.rowHeight = UITableViewAutomaticDimension;
self.tableView.estimatedRowHeight = 44.0;

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];

cell.leftLabel.text = ...;
cell.rightLabel.text = ...;

[cell setNeedsLayout];
[cell layoutIfNeeded];

return cell;
}

Snapshot of tableView with dynamic side-by-side labels



Related Topics



Leave a reply



Submit