当两个标签用砌体并排显示时,如何根据其内容显示每个标签的宽度?

我想用砌体在一行(视图)中显示两个标签(UILabel)。 但是左边的标签比右边的标签宽得多。 他们在这张照片上显示的是(绿色标签和黄色标签)

它们的内容压缩是UILayoutPriorityRequired,我希望左边标签的左边等于它的超级视图,右边的标签的右边等于它的超级视图。 那么如何让每个标签的宽度根据其内容显示更多的标签在视图中?

这是我的代码:

_label1 = [UILabel new]; _label1.backgroundColor = [UIColor yellowColor]; _label1.text = @"label,"; _label2 = [UILabel new]; _label2.backgroundColor = [UIColor greenColor]; _label2.text = @"label,"; [_contentView1 addSubview:_label1]; [_contentView1 addSubview:_label2]; [_label1 mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(_contentView1.mas_top).with.offset(5); make.left.equalTo(_contentView1.mas_left).with.offset(2); make.height.equalTo(@40); }]; [_label2 mas_makeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(_label1.mas_right).with.offset(2); make.top.equalTo(_contentView1.mas_top).with.offset(5); make.right.equalTo(_contentView1.mas_right).with.offset(-2); make.height.equalTo(@40); }]; [_label1 setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal]; [_label1 setContentCompressionResistancePriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal]; [_label2 setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal]; [_label2 setContentCompressionResistancePriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal]; 

http://swiftcn.io/topics/32设置内容拥抱和内容压缩。 (中国人吧?)