我使用以下代码来获取基于NSAttributedText的UITableViewCell的高度:
- (CGFloat)tableView:(UITableView *)pTableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
{
CGSize size = CGSizeMake([pTableView bounds].size.width, 100.0F);
NSAttributedString *text = [self.tableSource getData:[indexPath row]];
CGRect rect = [text boundingRectWithSize:size options:(NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading) context:nil];
return rect.size.height;
}
属性文本具有以下属性:
Country *c = [countryArray objectAtIndex:row];
NSMutableAttributedString *data = [[NSMutableAttributedString alloc] initWithString:c.name];
[data addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Palatino" size:30.0F] range:NSMakeRange(0, [c.name length])];
NSMutableParagraphStyle *paraStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopyWithZone:nil];
paraStyle.headIndent = 2.0F;
paraStyle.tailIndent = -2.0F;
paraStyle.lineSpacing = 20.0F;
[data addAttribute:NSParagraphStyleAttributeName value:paraStyle range:NSMakeRange(0, [c.name length])];
return data;
单线细胞给我33.0F的高度,有些给我高度53.0F。两个带衬里的细胞给我更多的高度,但它们是一致的。为什么单线电池的高度不一致33.0F和53.0F?
答案 0 :(得分:0)
我通过使用最小行高得到了预期的效果:
NSMutableParagraphStyle *paraStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopyWithZone:nil];
paraStyle.minimumLineHeight = 46.0F;
[data addAttribute:NSParagraphStyleAttributeName value:paraStyle range:NSMakeRange(0, [c.name length])];