前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >浅谈 iOS AutoLayout 中 Label 的抗拉伸和抗压缩

浅谈 iOS AutoLayout 中 Label 的抗拉伸和抗压缩

作者头像
s_在路上
发布2018-09-11 17:15:59
4.8K0
发布2018-09-11 17:15:59
举报
文章被收录于专栏:iOS 开发杂谈iOS 开发杂谈

UIView 中关于 Content HuggingContent Compression Resistance 的方法有:

代码语言:javascript
复制
- (UILayoutPriority)contentHuggingPriorityForAxis:(UILayoutConstraintAxis)axis NS_AVAILABLE_IOS(6_0);
- (void)setContentHuggingPriority:(UILayoutPriority)priority forAxis:(UILayoutConstraintAxis)axis NS_AVAILABLE_IOS(6_0);

- (UILayoutPriority)contentCompressionResistancePriorityForAxis:(UILayoutConstraintAxis)axis NS_AVAILABLE_IOS(6_0);
- (void)setContentCompressionResistancePriority:(UILayoutPriority)priority forAxis:(UILayoutConstraintAxis)axis NS_AVAILABLE_IOS(6_0);

Autolayout 优先级的范围是 1 ~ 1000,创建一个约束,默认的优先级是最高的 1000

Content Hugging Priority

Content Hugging Priority: 该优先级表示一个控件抗被拉伸的优先级。优先级越高,越不容易被拉伸,默认是251。

使用场景: 当一个视图上有多个 intrinsic content size 的子控件,子视图的总和,不够填充父视图区域时,此属性可以控制优先拉伸哪个视图内容。

Content Compression Resistance Priority

Content Compression Resistance Priority: 该优先级和上面那个优先级相对应,表示一个控件抗压缩的优先级。优先级越高,越不容易被压缩,默认是750。

使用场景: 当一个视图上有多个 intrinsic content size 的子控件,并且子控件可能会超出父视图的区域时,此属性可控制哪些视图被内容被优先压缩,使其不超出父视图区域。

举例说明

Content Compression Resistance Priority

View 中添加了一个 UILabel

代码语言:javascript
复制
- (void)demo1 {
    
    UILabel *yellowLabel = [[UILabel alloc] init];
    yellowLabel.text = @"我是黄色Label,我是黄色Label,我是黄色Label,我是黄色Label";
    yellowLabel.backgroundColor = [UIColor yellowColor];
    [self.view addSubview:yellowLabel];
    
    [yellowLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self.view);
        make.left.equalTo(self.view).offset(100);
        make.right.equalTo(self.view).offset(-100);
    }];
}

image.png

从最后的显示效果来看,中间的 Label 被压缩了。因为左右约束的优先级比固有内容相关的优先级要高,所以 Autolayout 布局的时候会优先满足左右两个约束。这时候:左边约束宽度 + 右边约束宽度 + Label 的固有内容宽度 > 屏幕宽度。所以最后只能压缩 Label 显示的宽度。

修改 View 左边约束和右边约束的优先级,或者只修改左(右)边约束优先级,然后设置 Label 抗压缩的优先级。

代码语言:javascript
复制
- (void)demo1 {
    
    UILabel *yellowLabel = [[UILabel alloc] init];
    yellowLabel.text = @"我是黄色Label,我是黄色Label";
    yellowLabel.backgroundColor = [UIColor yellowColor];
    [self.view addSubview:yellowLabel];
    [yellowLabel setContentCompressionResistancePriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];
    [yellowLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self.view);
        make.left.equalTo(self.view).offset(100).priority(250);
        make.right.equalTo(self.view).offset(-100).priority(250);
    }];
    
}

image.png

这时候 Label 控件的抗压缩约束优先级比右边约束优先级高,Autolayout 先满足 Lable 控件的固有内容 Size 的宽度,然后再满足左边和右边约束,表现出来就是 Lable 抗压缩特性变强了,它更倾向于显示它固有内容 Size,这时候被压缩的就是左边和右边的约束。

Content Hugging Priority

View 中添加了一个 UILabel

代码语言:javascript
复制
- (void)demo2 {
    
    UILabel *bluelabel = [[UILabel alloc] init];
    bluelabel.text = @"我是蓝色Label";
    bluelabel.backgroundColor = [UIColor blueColor];
    [self.view addSubview:bluelabel];
    
    [bluelabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self.view);
        make.left.equalTo(self.view).offset(100);
        make.right.equalTo(self.view).offset(-100);
    }];
}

image.png

拉伸和压缩的时候类似,左右约束优先级比 LabelContent Hugging Priority 优先级高,并且此时:左边约束宽度 + 右边约束宽度+ Label 的固有内容宽度 < 屏幕宽度。为了满足左右两个约束,就只有拉伸 Label

代码语言:javascript
复制
- (void)demo2 {
    
    UILabel *bluelabel = [[UILabel alloc] init];
    bluelabel.text = @"我是蓝色Label";
    bluelabel.backgroundColor = [UIColor blueColor];
    [self.view addSubview:bluelabel];
    [bluelabel setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];
    [bluelabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self.view);
        make.left.equalTo(self.view).offset(100).priority(250);
        make.right.equalTo(self.view).offset(-100).priority(250);
    }];
}

image.png

这时候 Label 控件的抗拉伸约束优先级比右边约束优先级高,Autolayout 先满足 Lable 控件的固有内容 Size 的宽度,然后再满足左边和右边约束,表现出来就是 Lable 抗拉伸特性变强了,它更倾向于显示它固有内容 Size,这时候被拉伸的就是左边和右边的约束。

Label 的抗拉伸和抗压缩

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2016.08.23 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Content Hugging Priority
  • Content Compression Resistance Priority
  • 举例说明
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档