图文混排计算文字高度的代码
//获取string的高度 (限定宽度为width, 字体大小为fontSize)
-(CGFloat)heightWithString:(NSString *)string
width:(CGFloat)width
fontSize:(CGFloat)fontSize
{
CGRect rect = [string boundingRectWithSize:CGSizeMake(width, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:fontSize]} context:nil];
return rect.size.height;
}
//设置cell的高度
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
//return 100.0;
TweetModel *model = _dataArray[indexPath.row];
float bodyLabelHeight = [self heightWithString:model.body width:250 fontSize:14] + 10;
//
float imgSmallHeight = 0;
if(model.imgSmall.length != 0)
{
imgSmallHeight = 50;
}
return 35 + (bodyLabelHeight+10) + imgSmallHeight + 35;
}