Multiple labels on screen with different size, color and style

Description

Creating multiple label widget on single screen with different size, style and color

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

STM32 MCU

What LVGL version are you using?

8.3

What do you want to achieve?

I am trying to add multiple labels (lv_label_create) on single screen with different style. I am using lv_style_t object for style. Not sure if this is proper way to do.

What have you tried so far?

Tried to use single object of lv_obj_t and lv_style_t but last style gets applied to all label on screen.
Tried to use array of lv_obj_t object and single lv_style_t object and same observation as above.

Code to reproduce

lv_style_t obj_style;
for( k =0 ; k <5 ; k++ )
{
   lv_obj_t * label = lv_label_create(lv_scr_act());
   lv_style_init(&obj_style);
   lv_obj_add_style(label, &obj_style, LV_PART_MAIN);
   lv_color_t clr = lv_color_hex(color[k]);
   lv_style_set_text_color(&obj_style, clr);
   lv_style_set_text_font(&text_style, &font[k]);
   lv_label_set_text(label,labeltext[k]);
   lv_obj_align(label, LV_ALIGN_TOP_LEFT, x[k], y[k]);
}

/*Another approach */
lv_style_t obj_style;
lv_obj_t * label[5]; 
for( k =0 ; k <5 ; k++ )
{
   label[k] = lv_label_create(lv_scr_act());
   lv_style_init(&obj_style);
   lv_obj_add_style(label[k], &obj_style, LV_PART_MAIN);
   lv_color_t clr = lv_color_hex(color[k]);
   lv_style_set_text_color(&obj_style, clr);
   lv_style_set_text_font(&text_style, &font[k]);
   lv_label_set_text(label[k],labeltext[k]);
   lv_obj_align(label[k], LV_ALIGN_TOP_LEFT, x[k], y[k]);
}

Screenshot and/or video

Last style of label gets applied to all labels on screen and different styles are not seen. Not sure what is proper way to handle this like array of lv_obj_t and lv_style_t.

This is normal - you add common style lv_style_t obj_style; for all created ob jects. And when you change settings of this style these settings applyed to every object associated with this style.
If you want unique style for every obect - use separate style objects for this. Or you can use local styles and change style properties of every object.

@glory-man Thanks for the clarification.
Is there any way to create style object dynamically like label object (lv_label_create(lv_scr_act()))? Because it might be possible that number of elements varies, and I don’t want to take array of lv_style_t obj_style[5].

Every object has style with some default properties. You can use object local style and set properties you need in objects you need. Local styles ©can't be shared among other objects. They are isolated by a specific object.

©

Local styles are like normal styles, but they can’t be shared among other objects. If used, local styles are allocated automatically, and freed when the object is deleted. They are useful to add local customization to an object.