How to stretch label body to fit parent container


Is there a way to automatically stretch/fit a label body to match the size of the container?
I’ve set the container LV_FIT_TIGHT so it’s width matches the largest label, but how do I make the small labels stretch to match all the other long labels?

What MCU/Processor/Board and compiler are you using?

What do you want to achieve?

All labels to have the same width body.

What have you tried so far?

Code to reproduce

        fault_cont = lv_cont_create(home_page, NULL);
        lv_style_copy(&fault_cont_style, &lv_style_transp);
        lv_obj_set_auto_realign(fault_cont, true);
        fault_cont_style.body.padding.inner = 20;
        lv_obj_set_style(fault_cont, &fault_cont_style);
        lv_obj_set_click(fault_cont, false);
        lv_cont_set_fit(fault_cont, LV_FIT_TIGHT);
        lv_cont_set_layout(fault_cont, LV_LAYOUT_COL_L);
        lv_obj_align(fault_cont, home_page, LV_ALIGN_IN_LEFT_MID, 10, 0);

        fault_label = lv_label_create(fault_cont, NULL);
        lv_label_set_text(fault_label, "short label");
        lv_style_copy(&fault_label_style, &lv_style_pretty);
        fault_label_style.text.font = &lv_font_roboto_16;
        lv_label_set_body_draw(fault_label, true);
        lv_label_set_align(fault_label, LV_LABEL_ALIGN_CENTER);
        lv_label_set_style(fault_label, LV_LABEL_STYLE_MAIN, &fault_label_style);

        fault_label = lv_label_create(fault_cont, NULL);
        lv_label_set_text(fault_label, "this is a long label");
        lv_label_set_body_draw(fault_label, true);
        lv_label_set_align(fault_label, LV_LABEL_ALIGN_CENTER);
        lv_label_set_style(fault_label, LV_LABEL_STYLE_MAIN, &fault_label_style);

Screenshot and/or video


I don’t think the container will change the size of its children automatically, so I think it has to be done by hand.

Yes that’s what I’m asking, how to do it by hand? Is it possible to stretch the short label? I want the “short label” to match the same width as the “long label”

I tried this:
lv_cont_set_fit2(fault_label, LV_FIT_FILL, LV_FIT_TIGHT);
But it didn’t work.

lv_cont_xxx APIs should only be called on containers or objects which inherit their functionality from containers (like pages/scrollable areas of pages). Labels do not inherit from containers so calling that API will probably result in memory corruption somewhere/things not working properly anymore.

As far as I know, you would have to set its width manually (using lv_obj_set_width).

I can’t change the size of the label manually at all. I’ve tried lv_obj_set_width() and lv_obj_set_size(). Is there anything else in my code above that would prevent me from resizing it? Do any of the styles override my manual set_width()?

I’ll have to get @kisvegabor to comment on that.

Any ideas anyone?

Sorry, somehow I’ve missed this topic.

So by default you can’t set the label’s size because it has LV_LABEL_LONG_EXPAND lond mode. It automatically sets the labels’ size according to the text’s size.

So you should choose a different long_mode before changing the size manually (E.g. LV_LABEL_LONG_CROP).

Perfect, thanks!!!