Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Resize UITableViewCell to UILabel's height dynamically

I want to resize cell's height according to the label's height and label's height according to text. Or is there any way I can resize the cell's height according to the text entered in UITextView?

like image 282
Rahul Vyas Avatar asked Jun 18 '09 12:06

Rahul Vyas


1 Answers

THIS METHOD IS DEPRECATED SINCE iOS 7.0.

There is a UITableView delegate method called heightForRowAtIndexPath that is called before you create a cell or a table.

You could use the NSIndexPath passed to it to get the text at a specific row and use the sizeWithFont method from UIStringDrawing.h to compute a CGSize for that row.

For example:

CGSize size = [text sizeWithFont:font
                   constrainedToSize:maximumLabelSize
                   lineBreakMode:UILineBreakModeWordWrap];

And finally you would return size.height.

like image 65
Josh Vera Avatar answered Oct 19 '22 10:10

Josh Vera