How to place variable text at the center of label/ta?

I want place the heart rate value in the center of LITTLEVGL LABEL or TA object always. the value vary frequently, for example, 20, 40, 105,etc. when 3 number is displayed, it is in the center of object (with fix size ), but when 2 number or 1 number situation, it isn’t in the center. how can i do? my code is in following.

static void CreateHeartrateLabel(lv_obj_t parent)
{
if( heartrate )lv_obj_del(heartrate);
heartrate = lv_ta_create(parent,NULL);/
for display the heart rate. /
lv_style_copy(&style_heartrate, &tabview_style);
style_heartrate.text.font = &lv_font_roboto_28;
style_heartrate.text.color = LV_COLOR_WHITE;
#if 0
lv_label_set_style(heartrate,LV_LABEL_STYLE_MAIN,&style_heartrate);
lv_label_set_long_mode(heartrate,LV_LABEL_LONG_BREAK); /
width always is keep. /
lv_label_set_align(heartrate,LV_ALIGN_CENTER); /
text align center. /
lv_obj_set_size(heartrate, 60,24);
lv_label_set_text(heartrate,"–");
#else
lv_ta_set_style(heartrate,LV_TA_STYLE_BG,&style_heartrate);
lv_ta_set_cursor_type(heartrate,LV_CURSOR_NONE);
lv_ta_set_one_line(heartrate,true);
lv_ta_set_text_align(heartrate,LV_ALIGN_CENTER);
lv_ta_set_placeholder_text(heartrate, “–”);
lv_ta_set_max_length(heartrate, 3);
lv_ta_set_sb_mode(heartrate,LV_SB_MODE_OFF);
#endif
lv_obj_set_size(heartrate,60,24);
lv_obj_set_pos(heartrate,95,174);
//lv_obj_align_origo(heartrate, NULL, LV_ALIGN_CENTER, 0, 54+14); /
for adjust the */
}

watch_num1 ![watch_num2|240x240](

watch_num2

watch_num3

1 Like

just as above images. the text isn’t align in the center always, the 1 number and 2 number image isn’t okay.

Hi,
I also struggled to understand this comprehensively, but I found that if you call lv_obj_align every time you change the value (either lv_label_set_text or lv_label_set_static_text( pLbl, NULL ) it yeilds the behaviour you desire.

I relooked at this matter,
As an alternative to calling lv_obj_align every time you change the text value,
If you have LV_USE_OBJ_REALIGN defined as 1 in lv_config.h then you can do the following,

At creatiion of the label for example,

lv_obj_t * lbl = lv_label_create( lv_scr_act(), NULL );
lv_label_set_static_text( lbl, txtLbl ); 
lv_label_set_align( lbl, LV_LABEL_ALIGN_CENTER );
lv_obj_align( lbl, NULL, LV_ALIGN_CENTER, 0, 70 );
lv_obj_set_auto_realign( lbl, true );
2 Likes

Cool design! Can you share more detail about your project? Looks great!

I wrote about this in another post somewhere, but I can’t find it.

Essentially, when auto realign is disabled, changing a label’s value will keep the top left corner of the text in the same position (which will lead to it not being centered).

Either of the two methods @deonm mentioned will do what you want.

Thank u very much. your idea is ok, it work!!

It is a ECG+PPG watch ( 240x240 TFT screen + NORDIC 5240 BLUETOOTH SOC etc). The entire system include watch, mobile phone, cloud sever etc. totally, it is a classic IOT project.The watch act as the IOT terminal . I make use of LVGL to implement the GUI . the LVGL has also been used by another project. The GUI current is just a start point, and is be adjusted to be more cool.

It’d be great if you added a few lines about your projects to the References post. I can also add it if you send an image, a link a few lines of description.

Okey. :grinning: maybe a few days later. because of most of GUI is coded in PC with LINUX + GCC + make + VSCODE+ SDL2 ( base of eclipse simulation). It can run in the watch hardware smooth , but the recent code and GUI havn’t been flash to watch.

1 Like

Okay, take your time! :slight_smile: