参考:
根据字符口串长度,给定固定宽度,计算字符串所需的高度;比较准确;
#define CELL_CONTENT_MARGIN 10
#define KLEFT_MARGIN 8.F
#define KFONT_SIZE 19.f
- (CGFloat)tableView:tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
NSString *contentText = [[hisTodayArr objectAtIndex:indexPath.row+1] valueForKey:KCONTENT_KEY];
CGSize constraint = CGSizeMake(hisTodayTab.frame.size.width - KLEFT_MARGIN - (CELL_CONTENT_MARGIN * 2), 9999.0f);
NSDictionary * attributes = [NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:KFONT_SIZE] forKey:NSFontAttributeName];
NSAttributedString *attributedText =
[[NSAttributedString alloc]
initWithString:contentText
attributes:attributes];
CGRect rect = [attributedText boundingRectWithSize:constraint
options:NSStringDrawingUsesLineFragmentOrigin
context:nil];
CGSize size = rect.size;
return MAX(size.height + 2*CELL_CONTENT_MARGIN, 44.0f);
}
下面这种是ios7出来之前的,ios7出来后就duplicate(建义不要再使用);本人测试过,当ios6和ios7出来的尺寸效果不同;
#define KTOP_MARGIN 5
#define KLAB_LAB_INTERVAL 5
#define KBOTTOM_MARGIN 5
#define KDETAIL_LAB_HEIGHT 18
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
// CGFloat labelWidht = bgView.frame.size.width - KIMAGEVIEW_WIDTH - KLAB_IMG_INTERVAL;
NSString *tmp = [[videoArray objectAtIndex:indexPath.section] valueForKey:BF_VIDEO_TITLE];
CGSize textSize =[tmp sizeWithFont:[UIFont systemFontOfSize:14.f] constrainedToSize:CGSizeMake(280.f, 60.f) lineBreakMode:NSLineBreakByCharWrapping];
return (KTOP_MARGIN + textSize.height + KLAB_LAB_INTERVAL + KDETAIL_LAB_HEIGHT + KBOTTOM_MARGIN);
// return 72.0f;
}