Uitableviewcell Auto Height Based on Amount of Uilabel Text

UITableViewCell auto height based on amount of UILabel text

To go a little further on Dao's answer..

He is correct, you need to use UITableViewAutomaticDimension

Also, you need to ensure that you setup your constraints in a way that all of the content in the cell is constrained to the cells contentView. So your label will likely need constraints such as

  • Leading constraint to ImageView
  • Top constraint to contentView
  • Bottom constraint to contentView
  • Trailing constraint to contentView

Make sure that you set the UILabel to multiline (or lines = 0) and it should work.

If you are using the heightForRowAt delegate functions ensure you return UITableViewAutomaticDimension

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.

How to adjust UITableView's height based on UILabel's height?

You should set numberoflines to 0. Provide either leading & trailing space constraints for your label or fixed width constraint along with top & bottom space constraints. Then use tableView.estimatedRowHeight = 44.0
tableView.rowHeight = UITableViewAutomaticDimension

Dynamically change cell height according to label text

The bottom anchor needs to be set:

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

Change the UITableViewCell Height According to Amount of Text

Based on the code you have provided, I think you are increasing only the cell height and not the cell.textLabel's height.

Ideally, you should set the frame size of cell.textLabel and the cell for you to see the full text in the cell.

A neat way to see whats wrong with a view in terms of size, is to color it different than the background (try setting cell.textLabel background to yellow) and see if the height is actually being set.

Here's how it should be

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
cell.textLabel.lineBreakMode = NSLineBreakByWordWrapping;
cell.textLabel.numberOfLines = 0;
cell.textLabel.font = [UIFont fontWithName:@"Helvetica" size:17.0];

NSString *cellText = @"Go get some text for your cell.";
UIFont *cellFont = cell.textLabel.font;
CGSize constraintSize = CGSizeMake(280.0f, MAXFLOAT);
CGSize labelSize = [cellText sizeWithFont:cellFont constrainedToSize:constraintSize lineBreakMode:UILineBreakModeWordWrap];
cell.textlabel.frame.size = labelSize;
cell.text = cellText;
}

Hope this helps!

update: This is quite an old answer, and many lines in this answer may be deprecated.

Constraining UIImageView height to respect dynamic UILabel height in UITableViewCell

Try setting thumbnail vertical compression resistance lower than title vertical hugging priority. In code it'll look like this:

    thumbnail.setContentCompressionResistancePriority(.defaultLow, for: .vertical)
titleLabel.setContentHuggingPriority(.required, for: .vertical)


Related Topics



Leave a reply



Submit