Hi Boss, and welcome to the Dev Forum!
I'm not sure I understand what you're asking, but I think it's something like: Given a multi-line UILabel object of constant width and font size, how can I set its height to fit variable length text, and how can I set the height of a containing table view cell to fit the label?
If I have the question right, here are the steps to take:
In cellForRowAtIndexPath:
Cell creation branch:
1) label.numberOfLines = 0;
2) label.lineBreakMode = UILineBreakModeWordWrap;
Settings for all cells:
3) Measure the text for the current row using
sizeWithFont:constrainedToSize: and apply height to label; e.g.:
// setup label
UILabel *label = [cell viewWithTag:kLabelTag];
NSDictionary *dictionaryForRow = [dataArray objectAtindex:indexPath.row];
NSString *text = [dictionaryForRow valueForKey:@"Text"];
UIFont *font = [UIFont systemFontOfSize:kFontSize];
CGSize size = [text sizeWithFont:font constrainedToSize:CGSizeMake(kCellWidth, 10000);
label.frame = CGRectMake(0, kVerticalMargin/2, size.width, size.height);
label.text = text;
4) In heightForRowAtIndexPath:
- (CGFloat)tableView:(UITableView *)tableView
heightForRowAtIndexPath:(NSIndexPath *)indexPath {
// repeating calculation in cellForRowAtInPath for simplicity, but for performance
// of course, this could be done once when the data array is loaded and the size
// stored in the dictionary for each row
NSDictionary *dictionaryForRow = [dataArray objectAtindex:indexPath.row];
NSString *text = [dictionaryForRow valueForKey:@"Text"];
UIFont *font = [UIFont systemFontOfSize:kFontSize];
CGSize size = [text sizeWithFont:font constrainedToSize:CGSizeMake(kCellWidth, 10000);
return size.height + kVerticalMargin;
}
The examples aren't taken from working code, btw. They're only intended to illustrate the basic steps. Here's a related thread that may be of interest to you: [http://discussions.apple.com/message.jspa?messageID=9541555#9541555].
Hope that helps!
- Ray