Files
knowledge-kit/Chapter1 - iOS/1.57.md
2020-02-25 17:46:51 +08:00

72 lines
3.8 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# 自动布局
## 1. iOS开发之AutoLayout中的Content Hugging Priority和 Content Compression Resistance Priority解析
- Content Hugging Priority直译成中文就是“内容拥抱优先级”从字面意思上来看就是两个视图谁的“内容拥抱优先级”高谁就优先环绕其内容。稍后我们会根据一些示例进行介绍。
- Content Compression Resistance Priority该优先级直译成中文就是“内容压缩阻力优先级”。也就是视图的“内容压缩阻力优先级”越大那么该视图中的内容越难被压缩。而该优先级小的视图则内容优先被压缩。稍后我们也会通过相应的实例来看一下这个优先级的具体表现。
这两个属性是可以在Storyboard中直接设置的选中要设置的控件在右边约束一栏里边就有Content Hugging Priority以及Content Compression Resistance Priority的设置地方。Content Hugging Priority的水平和竖直方向的默认值都是250而Content Compression Resistance Priority的水平和竖直的默认值是750。我们可以在此对该值进行设置。
假如要实现界面上 Label1、Label2、Label3。Label1宽度固定Label3右侧对齐且Label3必须显示完全Label2距离左侧5px右侧和Label3连在一起当Label3文字较多的时候Label2显示不全显示省略号Label3文字较少则Label2完全显示
![Label3文字较多](https://raw.githubusercontent.com/FantasticLBP/knowledge-kit/master/assets/2019-04-06-autolayout1.jpg)
![Label3文字较少](https://raw.githubusercontent.com/FantasticLBP/knowledge-kit/master/assets/2019-04-06-autolayout2.jpg)
下面是代码实现。
```Objective-c
UILabel *label1 = [[UILabel alloc] initWithFrame:CGRectZero];
label1.text = @"生生世世";
label1.font = [UIFont systemFontOfSize:15];
label1.textColor = [UIColor brownColor];
label1.textAlignment = NSTextAlignmentLeft;
label1.backgroundColor = [UIColor yellowColor];
[self.view addSubview:label1];
[label1 mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.view);
make.top.equalTo(self.view).offset(100);
make.width.mas_equalTo(64);
make.height.mas_equalTo(30);
}];
UILabel *label2 = [[UILabel alloc] initWithFrame:CGRectZero];
label2.text = @"杭城小刘杭城小刘杭城小刘杭城小刘杭城小刘杭城小刘杭城小刘";
label2.textColor = [UIColor purpleColor];
label2.textAlignment = NSTextAlignmentLeft;
label2.backgroundColor = [UIColor grayColor];
[self.view addSubview:label2];
// [label2 setContentHuggingPriority:UILayoutPriorityDefaultLow forAxis:UILayoutConstraintAxisHorizontal];
[label2 setContentCompressionResistancePriority:UILayoutPriorityDefaultLow forAxis:UILayoutConstraintAxisHorizontal];
[label2 mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(label1.mas_right).offset(5);
make.top.equalTo(self.view).offset(100);
make.height.mas_equalTo(30);
}];
UILabel *label3 = [[UILabel alloc] initWithFrame:CGRectZero];
label3.text = @"刘";
label3.textColor = [UIColor redColor];
label3.textAlignment = NSTextAlignmentRight;
label3.backgroundColor = [UIColor blueColor];
[self.view addSubview:label3];
[label3 setContentCompressionResistancePriority:UILayoutPriorityDefaultHigh forAxis:UILayoutConstraintAxisHorizontal];
// [label3 setContentHuggingPriority:UILayoutPriorityDefaultHigh forAxis:UILayoutConstraintAxisHorizontal];
[label3 mas_makeConstraints:^(MASConstraintMaker *make) {
make.right.equalTo(self.view).offset(-10);
make.top.equalTo(self.view).offset(100);
make.height.mas_equalTo(30);
make.left.equalTo(label2.mas_right);
}];
```