在自定义 的cell.h里暴露一个方法
+ (CGFloat)cellHeight:(NSString*)text;
在自定义 的cell.m里实现方法
+ (CGFloat)getStringHeight:(NSString*)string font:(CGFloat)fontSize {
CGRect rect = [string boundingRectWithSize:CGSizeMake(SCREEN_WIDTH-40, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:fontSize]} context:nil];
return ceil(rect.size.height);
}
+ (CGFloat)cellHeight:(NSString*)text {
return [CatHotTitleViewCell getStringHeight:text font:15]+100;
}
- (void)awakeFromNib {
[super awakeFromNib];
//设置label是多行显示
self.contentLabel.numberOfLines = 0;
}
//set方法赋值
- (void)setStrText:(NSString*)strText{
CGFloat height = [CatHotTitleViewCell getStringHeight:strText font:15];
self.contentLabel.frame=CGRectMake(20,0,SCREEN_WIDTH-40, height);
self.contentLabel.text= strText;
}
+ (CGFloat)getStringHeight:(NSString*)string font:(CGFloat)fontSize {
CGRect rect = [string boundingRectWithSize:CGSizeMake(SCREEN_WIDTH-40, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:fontSize]} context:nil];
return ceil(rect.size.height);
}
tabelview的使用返回cell的高度
- (CGFloat)tableView:(UITableView*)tableView heightForRowAtIndexPath:(NSIndexPath*)indexPath{
NSString*str =dataSourceArr[indexPath.row];
return [CatHotTitleViewCell cellHeight:str];
}
- (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath{
NSString*str =dataSourceArr[indexPath.row];
CatHotTitleViewCell *titleCell = [tableView dequeueReusableCellWithIdentifier:KCatHotTitleViewCellID];
titleCell.strText= str;
returntitleCell;
}
网友评论