iOS UILable自适应高度

//传入lable的宽度  文本内容 字体大小

+(CGFloat)stringHeightWithWidth:(int)width ForFont:(UIFont *)font string:(NSString *)string

{

    return [string boundingRectWithSize:CGSizeMake(width, 10000) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:font} context:nil].size.height;

}


//两个字体大小要一致

    CGFloat height = [NSString stringHeightWithWidth:width ForFont:[UIFont systemFontOfSize:17] string:titleStr] + 20;

    self.answerLable = [[UILabel alloc] initWithFrame: CGRectMake(20, 0, kScreenWidth - 40, height)];

    self.answerLable.font = [UIFont systemFontOfSize:17];


你可能感兴趣的:(iOS)