Content Hugging Priority and Content Compression resistance Priority

概念

Conent Hugging *

内容抱紧:在此基础上不再变大

Content Compression
内容压缩:在此基础上不再变小*

举个栗子

view上面放两个UILabel,两个Label水平,且上下左右都有约束。

  • First step 对这两个约束不做任何改动,如下设置
    self.leftLabel.text = @"aaaaaaaaaaaa";
    self.leftLabel.textColor = [UIColor redColor];
    self.rightLabel.text = @"bbbbb";
    self.rightLabel.textColor = [UIColor greenColor];
左边是leftLabel、右边是rightLabel.png

可见:默认情况下,右边的都展示出来了,而左边的没有都展示出来。因为苹果为了保证约束,牺牲了Label内容的大小,用...来表示了

  • Second step 修改两个Label水平方向上的Content Compression的优先级,让leftLabel小于rightLabel,让rightLabel不被压缩
leftLabel content compression 约束的优先级小于 rightLabel.png

可见:rightLabel都展示出来了,而leftLabel的内容被牺牲了,满足了约束的需求。

Tip:其实还有种方法能满足上面的需求

rightLabel的右侧约束的优先级改成比content compression还低,苹果会牺牲该约束的长度,来满足需求。效果和上图一样。
size :{ 100, 30 }
horizontal/vertical compression resistance priority of 750
and horizontal/vertical content hugging priority of 250
four constraints will be generated:

H:[label(<=100@250)]
H:[label(>=100@750)]
V:[label(<=30@250)]
V:[label(>=30@750)]

也就是说compression最小不能小于某个值,hugging的最大不能大于某个值

你可能感兴趣的:(Content Hugging Priority and Content Compression resistance Priority)