UILabel fixed with / flexible height
Thanks a lot!
MacBook 4Gb, Mac OS X (10.6.2), iPhone 3G 3.1.2
Apple Intelligence is now available on iPhone, iPad, and Mac!
MacBook 4Gb, Mac OS X (10.6.2), iPhone 3G 3.1.2
// 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;
- (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;
}
// 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;
- (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;
}
UILabel fixed with / flexible height