又是裝機(jī)量少但依然蛋疼的iOS7的問題.
關(guān)于含有l(wèi)abel的cell的高度自適應(yīng)的問題,在iOS 8.0以上都能輕松解決.
在-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath里面返回height=UITableViewAutomaticDimension就可以了.
那么煩人的iOS7該怎么辦呢?
首先,在heightForRowAtIndexPath里面建立一個(gè)臨時(shí)用的label,UILabel * tempLabel=[[UILabel alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width-168, CGFLOAT_MAX)];//隨便寫反正不添加到cell上面
tempLabel.text=@"........"
height=[thisClass labelheight:tempLabel];
[return height];
實(shí)現(xiàn)方法
+ (CGSize)labelheight:(UILabel *)detlabel
{
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.lineSpacing = 5;// 字體的行間距
NSDictionary *attributes = @{
NSFontAttributeName:[UIFont systemFontOfSize:15],
NSParagraphStyleAttributeName:paragraphStyle
};
CGSize size = CGSizeMake([UIScreen mainScreen].bounds.size.width - 168, 1000);
CGSize contentactually = [detlabel.text boundingRectWithSize:size options:(NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading) attributes:attributes context:nil].size;
return contentactually;
}
有autoLayout可以這樣做
Using Auto Layout in UITableView for dynamic cell layouts & variable row heights
參考下歪果仁的做法吧