1.label的自适应高度的时候,如果知道了label的宽度,和行数,就可以用洗面的方法
self.titleStr = [[UILabel alloc]init];
self.titleStr.frame = CGRectMake(0, 18+25+10, self.frame.size.width, 0);
self.titleStr.numberOfLines = 2;
self.titleStr.font = [UIFont systemFontOfSize:11];
self.titleStr.textAlignment = NSTextAlignmentCenter;
self.titleStr.lineBreakMode = NSLineBreakByTruncatingTail;
self.titleStr.text = model.RecommendClassName;
[self.titleStr sizeToFit];
要注意的是一定要在设置完文字后再设置最后一句话,不然不起作用.
2.知道文字的宽度和大小,来计算文字的高度
UILabel * label2 = [[UILabel alloc]init];
label2.text = self.contenLabel.text;
CGRect tempRect = [label2.text boundingRectWithSize:CGSizeMake(kWidth-32, 2000) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:14]} context:nil];
label2.frame = CGRectMake(16, CGRectGetMaxY(label1.frame)+OnePixelLine, kWidth, tempRect.size.height + 20);
label2.textColor = [UIColor colorWithHexString:@"#666"];
label2.font = [UIFont systemFontOfSize:14];
label2.textAlignment = NSTextAlignmentCenter;
label2.numberOfLines = 0;
[self.view addSubview:label2];