美文网首页
iOS 根据 文字的高度来 返回cell的高度

iOS 根据 文字的高度来 返回cell的高度

作者: 冬日的太阳_c107 | 来源:发表于2018-09-27 10:49 被阅读0次

在自定义 的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;

}

相关文章

网友评论

      本文标题:iOS 根据 文字的高度来 返回cell的高度

      本文链接:https://www.haomeiwen.com/subject/zpasoftx.html