Label 用Masonry 怎么自适应宽度

1.宽度不够时

[_label1 setContentCompressionResistancePriority:UILayoutPriorityRequired
forAxis:UILayoutConstraintAxisHorizontal];

2.宽度够时

[label setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];

//显示多行,自适应高度
UILabel *label3 = [[UILabelalloc] initWithFrame:CGRectZero];
[self.viewaddSubview:label3];
label3.backgroundColor =[UIColorredColor];
label3.text =@"我爱北京天安门!天安门上太阳升!我爱北京天安门!天安门上太阳升!我爱北京天安门!天安门上太阳升!我爱北京天安门!天安门上太阳升!我爱北京天安门!天安门上太阳升!我爱北京天安门!天安门上太阳升!我爱北京天安门!天安门上太阳升!我爱北京天安门!天安门上太阳升!我爱北京天安门!天安门上太阳升!我爱北京天安门!天安门上太阳升!我爱北京天安门!天安门上太阳升!我爱北京天安门!天安门上太阳升!";
label3.preferredMaxLayoutWidth = (self.view.frame.size.width -10.0 * 2);
[label3 setContentHuggingPriority:UILayoutPriorityRequiredforAxis:UILayoutConstraintAxisVertical];
label3.numberOfLines =0;
[label3 mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.mas_equalTo(10.0);
make.right.mas_equalTo(-10.0);
make.top.mas_equalTo(100.0);
}];

推荐阅读更多精彩内容