Apple Intelligence is now available on iPhone, iPad, and Mac!

Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

UILabel fixed with / flexible height

I have an iPhone App that read an XML file on the web. I must put the readed data in a grouped UITableView with fixed content. My fixed content are some UILabels that have fixed width. When I put the text in a UILabel I wouldn't change the FontSize but, with WordWarp, extend its height in order to show all the text. My UILabel is in a cell and so I must change also the height of the cell.
Thanks a lot!

MacBook 4Gb, Mac OS X (10.6.2), iPhone 3G 3.1.2

Posted on Feb 19, 2010 4:37 AM

Reply
Question marked as Top-ranking reply

Posted on Feb 20, 2010 2:30 AM

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
2 replies
Question marked as Top-ranking reply

Feb 20, 2010 2:30 AM in response to BossOz

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

UILabel fixed with / flexible height

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.