label的抗压等级

如上图需求:

1.左侧label固定位置,不能压缩

2.中间label长度过长是可以被压缩

3.label3不能压缩,保持在label2的右侧,且不超过cell右侧

知识点:

1.uiview的方法,用于label2的压缩性能

- (void)setContentCompressionResistancePriority:(UILayoutPriority)priority forAxis:(UILayoutConstraintAxis)axis API_AVAILABLE(ios(6.0));

参数1 UILayoutPriority:抗压等级,越高越无法压缩,越低越会被压缩

UILayoutPriorityRequired                    无法压缩

UILayoutPriorityDefaultHigh

UILayoutPriorityDefaultLow

UILayoutPriorityFittingSizeLevel        可以压缩

参数2 UILayoutConstraintAxis:方向

UILayoutConstraintAxisHorizontal 水平方向

UILayoutConstraintAxisVertical 垂直方向

2.Masonry中的优先级,用于label3的布局

priorityHigh() 高优先级,布局有冲突时优先使用

priorityLow() 低优先级,布局有冲突时不会使用

代码如下:

 UIView *bg = [UIView new];
    bg.backgroundColor = [UIColor lightGrayColor];
    
    // 固定宽度
    UILabel *label1 = [UILabel new];
    label1.text = @"固定宽度";
    [bg addSubview:label1];
    [label1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(bg);
        make.left.equalTo(bg).offset(10);
        
    }];
//    [label1 setContentCompressionResistancePriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];//不可以被压缩,尽量显示完整
    
    // 可压缩不定长度文本
    UILabel *label2 = [UILabel new];
    label2.text = str;
    label2.textColor = [UIColor blueColor];
    [bg addSubview:label2];
    [label2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(label1.mas_right).offset(10);
        make.centerY.equalTo(bg);
    }];
    [label2 setContentCompressionResistancePriority:UILayoutPriorityFittingSizeLevel forAxis:UILayoutConstraintAxisHorizontal];//宽度不够时,可以被压缩

    
    // 靠左固定
    UILabel *label3 = [UILabel new];
    label3.text = @"置顶";
    label3.textColor = [UIColor redColor];
    label3.layer.borderColor = [UIColor redColor].CGColor;
    label3.layer.borderWidth = 1;
    label3.layer.cornerRadius = 5;
    [bg addSubview:label3];
    [label3 mas_makeConstraints:^(MASConstraintMaker *make) {
        // 在设置left和right的时候,需要对left使用高优先级,需要对right使用低优先级。
        make.left.equalTo(label2.mas_right).offset(10).priorityHigh();
        make.right.equalTo(bg).offset(-10).priorityLow();
        make.centerY.equalTo(bg);
        make.height.mas_equalTo(30);
    }];
    return  bg;

补充:

抗拉方法

- (void)setContentHuggingPriority:(UILayoutPriority)priority forAxis:(UILayoutConstraintAxis)axis API_AVAILABLE(ios(6.0));

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
要根据不同等级为多个 Label 绘制不同的颜色块,你可以使用 LVGL 的绘图功能和对象风格来实现。下面是一个示例代码,展示了如何在 LVGL 中为多个 Label 绘制不同的颜色块: ```c #include "lvgl/lvgl.h" #define NUM_LABELS 3 lv_obj_t *labels[NUM_LABELS]; lv_obj_t *color_blocks[NUM_LABELS]; void setup(void) { lv_obj_t *scr = lv_disp_get_scr_act(NULL); for (int i = 0; i < NUM_LABELS; i++) { labels[i] = lv_label_create(scr, NULL); lv_label_set_text(labels[i], "Label"); lv_obj_align(labels[i], LV_ALIGN_CENTER, 0, i * 40); // 设置 Label 的位置 color_blocks[i] = lv_obj_create(scr, NULL); lv_obj_set_size(color_blocks[i], 20, 20); // 设置颜色块的大小 lv_obj_align(color_blocks[i], LV_ALIGN_IN_LEFT_MID, 10, i * 40); // 设置颜色块的位置 } lv_obj_set_style_bg_color(color_blocks[0], lv_palette_main(LV_PALETTE_RED), 0); lv_obj_set_style_bg_color(color_blocks[1], lv_palette_main(LV_PALETTE_YELLOW), 0); lv_obj_set_style_bg_color(color_blocks[2], lv_palette_main(LV_PALETTE_GREEN), 0); } int main(void) { lv_init(); lv_disp_drv_t disp_drv; lv_disp_drv_init(&disp_drv); disp_drv.disp_flush = your_flush_cb; // 设置你的显示驱动回调函数 lv_disp_drv_register(&disp_drv); setup(); while (1) { lv_task_handler(); your_event_handler(); // 处理你的事件 } return 0; } ``` 在上面的示例代码中,我们创建了多个 Label 对象和对应的颜色块对象。通过循环设置它们的位置和初始颜色块的背景色。然后,我们使用 `lv_obj_set_style_bg_color` 函数为每个颜色块对象设置不同的背景色。 你可以根据需要修改示例中的参数,比如 Label 的数量、位置和颜色块的大小。同时,你还需要根据自己的具体情况实现显示驱动回调函数和事件处理函数。 希望这个示例对你有所帮助!如有任何疑问,请随时提问。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值