如何修复溢出其包含UITableViewCell的多行UILabel?

时间:2009-06-11 23:44:07

标签: iphone uitableview uilabel

我在自定义UITableView单元格中显示多行UILabel时遇到问题。

我正在使用它来计算单元格的高度...

NSString *cellText = [howtoSection objectAtIndex:row];
UIFont *cellFont = [UIFont fontWithName:@"Helvetica" size:15.0];
CGSize constraintSize = CGSizeMake(260.0f, MAXFLOAT);
CGSize labelSize = [cellText sizeWithFont:cellFont constrainedToSize:constraintSize lineBreakMode:UILineBreakModeWordWrap];

return labelSize.height + 20;

......这是标签本身。

// Calc the height
NSString *cellText = [howtoSection objectAtIndex:row];
CGSize constraintSize = CGSizeMake(260.0f, MAXFLOAT);
CGSize labelSize = [cellText sizeWithFont:cell.textLabel.font constrainedToSize:constraintSize lineBreakMode:cell.textLabel.lineBreakMode];

// Create the label frame
CGRect newFrame = cell.contentLabel.frame;
newFrame.size.height = labelSize.height;
cell.contentLabel.frame = newFrame;

[cell.contentLabel setText:[howtoSection objectAtIndex:row]];

除了将标签向下推出并从其单元格中移出外,所有内容都按计划运行。如果不是因为这个明显的上边距一切都适合。

这是我在模拟器中看到的图片的链接...

iPhone Rendering Bug

非常感谢任何帮助。

2 个答案:

答案 0 :(得分:1)

我没有看到您在显示的代码中设置cell.contentLabel.font

此外,标签尺寸计算使用cell.textLabel.font来计算其尺寸,但使用contentLabel变量进行渲染。

您是否可能使用与计算不同的字体进行渲染?

答案 1 :(得分:0)

您如何将标签添加到contentView?看起来原始定位是错误的,因为高度看起来要正确计算。如果您注释掉指定新框架,标签是否位于正确的位置?我的赌注是它不是。