一、正常计算行高
这其中需要先传入一个最大尺寸和一个属性字典,特殊的格式要求都写在属性字典中。
NSDictionary *attrs = @{NSFontAttributeName : font};
[str boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:attrs context:nil].size
二、预估行高
iOS8的新特性
需要适配iOS7的行高计算
_tableView.estimatedRowHeight = 30;
_tableView.rowHeight = UITableViewAutomaticDimension;
三、系统自动计算
iOS 6.0以后都支持
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *identify = NSStringFromClass([Case8Cell class]);
Case8Cell *cell = [tableView dequeueReusableCellWithIdentifier:identify];
if (!cell) {
cell = [[Case8Cell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:identify];
}
Case8DataEntity *entity = _arrayEntity[indexPath.row];
[cell setEntityData:entity indexPath:indexPath click:^() {
}];
//缓存行高
if (entity.cellHeight <= 0) {
entity.cellHeight = [cell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height + 0.5f;
}
if (entity.cellHeight < 50) {
return 50;
}
return entity.cellHeight;
}