1.计算文本的size:
/*
*简单计算textsize
*@param width传入特定的宽度
*@param font字体
*/
- (CGSize)sizeWithLabelWidth:(CGFloat)width font:(UIFont*)font;
- (CGSize)sizeWithLabelWidth:(CGFloat)width font:(UIFont*)font{
NSDictionary*dict=@{NSFontAttributeName: font};
CGRectrect=[selfboundingRectWithSize:CGSizeMake(width,MAXFLOAT)options:(NSStringDrawingUsesLineFragmentOrigin)attributes:dictcontext:nil];
CGFloatsizeWidth=ceilf(CGRectGetWidth(rect));
CGFloatsizeHieght=ceilf(CGRectGetHeight(rect));
returnCGSizeMake(sizeWidth, sizeHieght);
}
2.在自定义cell中,布置各个控件的frame
- (void)layoutSubviews{
[superlayoutSubviews];
//限制宽度
CGFloat statusLabelWidth =self.frame.size.width-20;
//根据实际内容,返回高度,
CGSize statusLabelSize = [self.titleLable.textsizeWithLabelWidth:statusLabelWidthfont:[UIFontsystemFontOfSize:17]];
//根据statusLabelSize设置各个空间的frame;
self.titleLable.frame=CGRectMake(10,5,self.bounds.size.width-20, statusLabelSize.height);
//分割线
UIView*view = [[UIViewalloc]initWithFrame:CGRectMake(5,statusLabelSize.height+140,self.bounds.size.width-10,0.5)];
view.backgroundColor= [UIColorgrayColor];
[self.contentViewaddSubview:view];
3.contrller中实现动态返回cell高度
- (CGFloat)tableView:(UITableView*)tableView heightForRowAtIndexPath:(NSIndexPath*)indexPath{
//文本内容
NSString*status =_titleArray[indexPath.row];
CGFloat statusLabelWidth =mScreenWidth-20;
CGSize statusSize = [statussizeWithLabelWidth:statusLabelWidthfont:[UIFontsystemFontOfSize:17]];
CGFloat iconHeight =140;//其他控件的高度
return statusSize.height + iconHeight;
}
网友评论