I have a subclassed UITableViewCell called Comment, which has a UILabel containing some relevant information. This information is of varying length, so it is merely impossible for me to set a appropriate height for the UILabel.
I have tried making the cell dynamic, but the UILabel either overflows the cell (y axis) or the cell is too big.
Please can you tell me where I am going wrong?!
tableView... cellForRowAtIndexPath...
[cell->commentText setLineBreakMode:UILineBreakModeWordWrap];
[cell->commentText setMinimumFontSize:14];
[cell->commentText setNumberOfLines:0];
[cell->commentText setFont:[UIFont systemFontOfSize:14]];
NSString *text = [cell->commentText text];
CGSize constraint = CGSizeMake(320, 20000.0f);
CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:14] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];
[cell->commentText setText:text];
[cell->commentText setFrame:CGRectMake(20, 40, 280, MAX(size.height, 125))];
- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *text = [[[comments objectAtIndex:indexPath.row] body] stringByStrippingHTML];
CGSize constraint = CGSizeMake(320, 20000.0f);
CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:14] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];
CGFloat height = MAX(size.height, 125);
return height + 20;
}
Are you stripping the html in your
cellForRowAtIndexPath:also?I’ve dynamically resized labels two different ways which have both worked:
1) In IB, anchor the label to the top and bottom of the cell and set its height to flexible (under the ruler icon in the right side panel). Then, just implement
heightForRowAtIndexPathas you’ve done.2) In
cellForRowAtIndexPath, set the label’s width and then call[label sizeToFit]. Use your sameheightForRowAtIndexPath.