概念
Conent Hugging *
<pre>內(nèi)容抱緊:在此基礎(chǔ)上不再變大</pre>
Content Compression
<pre>內(nèi)容壓縮:在此基礎(chǔ)上不再變小*</pre>
舉個(gè)栗子
在view上面放兩個(gè)UILabel,兩個(gè)Label水平,且上下左右都有約束。
- First step 對(duì)這兩個(gè)約束不做任何改動(dòng),如下設(shè)置
<pre>self.leftLabel.text = @"aaaaaaaaaaaa";</br>self.leftLabel.textColor = [UIColor redColor];</br>self.rightLabel.text = @"bbbbb";</br>self.rightLabel.textColor = [UIColor greenColor];</pre>

左邊是leftLabel、右邊是rightLabel.png
可見:默認(rèn)情況下,右邊的都展示出來了,而左邊的沒有都展示出來。因?yàn)?strong>蘋果為了保證約束,犧牲了Label內(nèi)容的大小,用...來表示了
- Second step 修改兩個(gè)
Label水平方向上的Content Compression的優(yōu)先級(jí),讓leftLabel小于rightLabel,讓rightLabel不被壓縮

leftLabel content compression 約束的優(yōu)先級(jí)小于 rightLabel.png
可見:rightLabel都展示出來了,而leftLabel的內(nèi)容被犧牲了,滿足了約束的需求。
Tip:其實(shí)還有種方法能滿足上面的需求
把rightLabel的右側(cè)約束的優(yōu)先級(jí)改成比content compression還低,蘋果會(huì)犧牲該約束的長(zhǎng)度,來滿足需求。效果和上圖一樣。
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最小不能小于某個(gè)值,hugging的最大不能大于某個(gè)值