自适应高度cell

- (instancetype)initWithFrame:(CGRect)frame {

    if (self = [super initWithFrame:frame]) {

        _onlyLabel = [[UILabel alloc] initWithFrame:CGRectZero];

        [self.contentView addSubview:_onlyLabel];

    }

    return self;

}

 

- (void)layoutSubviews {

    [super layoutSubviews];

    _onlyLabel.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [MyTableViewCell heightForString:self.person.name]);

}

 

+ (CGFloat)heightForString:(NSString *)string {

    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:17], NSFontAttributeName, nil ];

    CGRect rect = [string boundingRectWithSize:CGSizeMake([UIScreen mainScreen].bounds.size.width, 10000) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];

    return rect.size.height;

}

 

+ (CGFloat)cellForStudent:(Person *)person {

    

    return 20 + [MyTableViewCell heightForString:person.name];

}

原文地址:https://www.cnblogs.com/naizui/p/5211579.html