Skip to content

Instantly share code, notes, and snippets.

@dhl613
Last active December 6, 2019 09:29
Show Gist options
  • Save dhl613/03a9c3576d9287518565dc25af2daeef to your computer and use it in GitHub Desktop.
Save dhl613/03a9c3576d9287518565dc25af2daeef to your computer and use it in GitHub Desktop.
UILabel 并排 自适应 显示
“Content Hugging Priority”,也叫内容紧靠优先级(小名:别扯我),该优先级越高,这越晚轮到被拉伸。
“Content Compression Resistance Priority”,也叫内容压缩阻力优先级(小名:别挤我),该优先级越高,则越晚轮到被压缩。
// ⤴️原文: https://www.jianshu.com/p/a4b8e0c8e68d
// 紧靠能力 优先级越低 内容显示得越完整 反之,内容被省略
[self.nameLabel setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];
[self.priceLabel setContentHuggingPriority:UILayoutPriorityDefaultLow forAxis:UILayoutConstraintAxisHorizontal];
// 抗压缩能力 优先级越高 内容显示得越完整 反之,内容被省略
[self.nameLabel setContentCompressionResistancePriority:UILayoutPriorityDefaultLow forAxis:UILayoutConstraintAxisHorizontal];
[self.priceLabel setContentCompressionResistancePriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];
/**
* 小结:
* Content Hugging Priority : 优先级越低 内容显示更完整,即优先显示
* Content Compression Resistance Priority : 优先级越高 内容显示更完整,即优先显示
* 设置优先级时最好再设置一个固定宽度(优先显示的label),并将该优先级设置低级
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment