主要设置一下两个地方
@property(nonatomic)CGFloat preferredMaxLayoutWidth
- (void)setContentHuggingPriority:(UILayoutPriority)priority forAxis:(UILayoutConstraintAxis)axis
label自适应前准备
UILabel *label = [[UILabelalloc] initWithFrame:CGRectZero];
[self.view addSubview:label];
label.text =@"高度自适应高度自适应高度自适应高度自适应高度自适应高度自适应高度自适应高度自适应高度自适应高度自适应";
label.numberOfLines =0;
label.font = [UIFont systemFontOfSize:14];
自动适配高度如下
// 1. 设置最大宽度
label.preferredMaxLayoutWidth = 1000;
// 2. 设置适配方式
[label setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];
// 3. 设置三条约束(左右上约束),高度自适应
[label mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.mas_equalTo(10.0);
make.right.mas_equalTo(-10.0);
make.top.mas_equalTo(10.0);
}];