iOS Calculate Text Height in Tableview Cell

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.

How to calculate tableView row height and pass value to heightForRowAtIndexPath

If your cell view is really very complex and every component's height are depending on data source. You can try to create the view in heightForRowIndexPath method and then cache the created view to a dictionary in your view controller and use it directly in cellForRowAtIndexPath. In this way you only need to create the view once when user scrolling the table. If the datasource is not changing very frequently, you can reuse the cached view in heightForRowIndexPath as well.

And if the tableview has a lot of rows, you should return an approximate value for height in estimatedHeightForRowAtIndexPath to speed up the loading process of the view controller. Otherwise during loading tableview, it will try to calculate all row's height which may requires a lot of time.

But I really don't think your cell would be so complex. If only some UITextLabels that depends on datasource for the height, you can simply only calculate the height for the label, then add it to other components' height which is fixed.

Precise calculation of the Height of a UITableViewCell Based on Text

Any chance the fonts don't match?

You'd be calculating based on Helvetica Neue Regular, I think.

What's the font of the UILabel? Being off by one word makes me think font metrics.

I don't see another error

Calculate Cell height on basis of label text + image

Auto Resizing of Cell is available in Ios 8.0, The issue my deployment target was ios 7.0, which is causing the layout issues.

Please refer to these articles:

http://www.appcoda.com/self-sizing-cells/

The Code is in swift but need to do same thing's in objective c as well.

This will also help's you.

http://useyourloaf.com/blog/self-sizing-table-view-cells.html

Change cell height by the content of the textView inside the cell

Bound Your textview with cell from all sides using marginal constraints.(Leading, Trailing, Top and Bottom constraints)

Sample Image

  • Disable textView Scrolling

In viewDidLoad() add the following.

tableView.estimatedRowHeight = 44.0
tableView.rowHeight = UITableViewAutomaticDimension

This will make your cell size according to your textview content size.

Have a look at result :

Sample Image

You don't need to write heightForRowAtIndexPath.



Related Topics



Leave a reply



Submit