这是我的代码:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
UIFont *cellFont;
if(delegate.iPad){
cellFont= [UIFont fontWithName:@"Helvetica" size:28.0];
}else{
cellFont= [UIFont fontWithName:@"Helvetica" size:12.0];
}
CGSize size = [cellText sizeWithAttributes:@{NSFontAttributeName: myFont}];
// Values are fractional -- you should take the ceilf to get equivalent values
CGSize labelSize = CGSizeMake(ceilf(size.width), ceilf(size.height));
tableHeight2 = tableHeight2 + labelSize.height;
NSStringDrawingContext *ctx = [NSStringDrawingContext new];
NSAttributedString *aString = [[NSAttributedString alloc] initWithString:cellText];
UITextView *calculationView = [[UITextView alloc] init];
[calculationView setAttributedText:aString];
CGRect textRect = [calculationView.text boundingRectWithSize:table2.frame.size options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:cellFont} context:ctx];
return = textRect.size.height;
}
文本的最后一行由于某种原因被切断,即使它似乎正在正确计算高度。我错过了什么吗?
答案 0 :(得分:0)
可能发生以下两个原因:
1) AutoLayout 或 AutoResize 问题,即设置不正确。
2)heightForRowAtIndexPath
中返回的高度是错误的。
解释:这里你应该返回高度(文字高度+顶部填充+底部填充)
return = textRect.size.height+(2*label.frame.origin.x);