父视图根据label的高度自适应调整:
需要设置label:
1、numberOfLines
2、preferredMaxLayoutWidth 或者 固定label的宽度
3、- (void)setContentHuggingPriority:(UILayoutPriority)priority forAxis:(UILayoutConstraintAxis)axis;
代码如下:
UIView *contentView = [[UIView alloc] init];
[self addSubview:contentView];
contentView.backgroundColor = [UIColor redColor];
[contentView mas_makeConstraints:^(MASConstraintMaker *make) {
make.center.equalTo(@0);
make.width.equalTo(@300);
}];
UILabel *label = [[UILabel alloc] init];
[contentView addSubview:label];
[label mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(@20);
// make.centerX.equalTo(@0);
make.bottom.equalTo(@-20);
make.left.equalTo(@50);
make.right.equalTo(@-50);
}];
label.numberOfLines = 0;
// label.preferredMaxLayoutWidth = 200;
[label setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];
label.backgroundColor = [UIColor whiteColor];
label.text = @"是打发士大夫撒的发生大法师打发斯蒂芬三大法师的法师大法师打发斯蒂芬三大法师的法大法师打发斯蒂芬三大法师的法";
网友评论