Center multiline label vertically


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

nRF5340 / st7735r

What LVGL version are you using?


What do you want to achieve?

I want a label to center vertically according to the (dynamic) text that is put inside.

What have you tried so far?

I have set the label to wrap.
I have set the height of the label to 1 line.
I have set the text (too long for one line, may span 2 to 5 lines) of the label:
lv_label_set_text(ui_TempMessageField, "looong text looong text looong text looong text looong text looong text looong text looong text looong text looong text looong text looong text ");

I have called :
int curheight = lv_obj_get_height(ui_TempMessageField);
But the height is always the initial height. I did not find any way to get the height of the inner text (or number of lines), which I could have used to move the label to center it vertically inside its parent.

Also I have not found any flag to set vertical centering.

Thanks for any tip to center a dynamic label vertically.

For whomever is interested, I managed to do this by:

  • setting the height to LV_SIZE_CONTENT
    lv_obj_set_height( ui_TempMessageField, LV_SIZE_CONTENT); /// 119

  • setting the content text
    lv_label_set_text(ui_TempMessageField, m_tmpmessage_str);

  • update the layout, get the new height, move vertically the field accordingly
    int hh = lv_obj_get_height(ui_TempMessageField);
    lv_obj_set_y(ui_TempMessageField, LV_VER_RES/2 - hh/2);