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

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

- (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

- (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);
    }];
}
复制代码

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

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

- (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);
    }];
    
}
复制代码

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

Content Hugging Priority

View 中添加了一个 UILabel

- (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);
    }];
}
复制代码

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

- (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);
    }];
}
复制代码

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

Label 的抗拉伸和抗压缩

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值