First thing to do is calculate the UITableViewCell height inside heightForRowAtIndexPath:(NSIndexPath)indexPath. That's the first of the two methods that we have to implement for UITableViewDataSource protocol in order to calculate the total of cell height. Be careful because you have to add all cell heights for any control that are docked vertically.
Obtaining the text size of the label is quite easy, we only have to specify the label widht, the font size, line break mode and text content, and we'll obtain a CGSize variable storing the label size.
CGSize labelSize = CGSizeMake(labelWidth, 20000.0f);Next, you have to implement cellForRowAtIndexPath method for dinamic resizing of the UILabel which shows our text. Using the same method described before you can set label's frame to show text completely.
CGSize textSize = [@"Lorem ipsum dolor" sizeWithFont:[UIFont systemFontOfSize: 12.0f] constrainedToSize:labelSize lineBreakMode:UILineBreakModeWordWrap];
cellHeight = textSize.height + 21.0f + 30; //Adding other control heights
return cellHeight;
UILabel *textDescription = (UILabel*)[cell viewWithTag:3];
textDescription.text = @"Lorem ipsum dolor...";
CGSize labelSize = CGSizeMake(labelWidth, 20000.0f);
CGSize textSize = [rssItem.descripcion sizeWithFont:[UIFont systemFontOfSize: 12.0f] constrainedToSize:labelSize lineBreakMode:UILineBreakModeWordWrap];
[txtDescripcion setFrame:CGRectMake(13.0f, 45.0f,
labelWidht, textSize.height)];
And that's all to dynamic resizing of UILabels and UITableViewCells for show unknow length text. I hope this had helped you
NOTE: A good practise is to have the label widht defined as a constant, in our case labelWidth.
No comments:
Post a Comment