How to set label text in the center of label?

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

I’m using simulator to build my UI.

What LVGL version are you using?


What do you want to achieve?

I have a label with a border stretching my monitor’s maximum width. I want to place the label’s text in the center of the bounding border.

What have you tried so far?

I tried using lv_style_set_text_align and lv_style_set_align to set a style, and then apply the style to the label, but neither of them work. Also, calling lv_obj_set_align on the label doesn’t work as well.

Code to reproduce

  static lv_style_t main_angle_style;
  lv_style_set_border_width(&main_angle_style, 2);
  lv_style_set_radius(&main_angle_style, 5);
  lv_style_set_pad_all(&main_angle_style, 5);
  lv_style_set_text_align(&main_angle_style, LV_ALIGN_CENTER);
  lv_style_set_align(&main_angle_style, LV_ALIGN_CENTER);

  lv_obj_t *main_angle = lv_label_create(root);
  lv_label_set_text(main_angle, "180.000");
  lv_obj_add_style(main_angle, &main_angle_style, LV_PART_MAIN);
  lv_obj_set_align(main_angle, LV_ALIGN_CENTER);

Screenshot and/or video

Try this:

lv_obj_set_style_text_align(main_angle , LV_TEXT_ALIGN_CENTER, 0);

From here:

Thank you for the answer! It works now.

You’re welcome