I have a requirement to fill cells with varying length wraparound text. My current routine is handling this satisfactorily, however, I am concerned about the use of a couple of constants being used.
The values 10 and 260 below represent the margin and cell width expected, but they are only accurate for standard definition resolution in portrait orientation.
Is there some screen/table object that provides me these values as metrics I could use instead of the constants?
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell;
UILabel *lbl;
CGRect frame;
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero
reuseIdentifier:AnswerIdentifier] autorelease];
frame.origin.x = 10;
frame.origin.y = 10;
frame.size.height = [self textHeight:indexPath];
frame.size.width = 260;
lbl = [[UILabel alloc] initWithFrame:frame];
lbl.lineBreakMode = UILineBreakModeWordWrap;
lbl.numberOfLines = 0;
[cell.contentView addSubview:lbl];
[lbl release];
return cell;
}
- (CGFloat)textHeight:(NSIndexPath *)indexPath {
CGSize textSize;
CGSize constraintSize;
CGFloat height;
NSString *theText = [self cellText:indexPath];
constraintSize = CGSizeMake(260.0f, MAXFLOAT);
textSize = [theText sizeWithFont:kCELL_FONT
constrainedToSize:constraintSize
lineBreakMode:UILineBreakModeWordWrap];
height = textSize.height;
return height;
}