Help to resolve compile errors when changing label font size

Description

Hi, I am new to LVGL and have tried many of the excellent example widgets with success. I would now like to modify the default font size on a label. My project will have a number of labels which will require local changes to their font size.

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

Teensy 4.0 with Arduino IDE

What LVGL version are you using?

8.0.2

What do you want to achieve?

Locally change font size on labels

What have you tried so far?

I have added the code described in other forum posts but have errors when trying to compile. Without the added lines of code the sketch compiles and runs without problem. Any help would be much appreciated, thanks
Error message with the added lines shown in attached code;

  1. cannot convert ‘anonymous enum’ to ‘const lv_font_t* {aka const_lv_font_t*}’ for argument ‘2’ to ‘void lv_style_set_text_font(lv_style_t*, const lv_font_t*)’
  2. cannot convert ‘anonymous enum’ to lv_style_t* for argument ‘2’ to ‘void lv_obj_add_style(_lv_obj_t*, lv_style_t*, lv_style_selector_t)’
/* static void event_handler_1(lv_event_t * event)
{
  LV_LOG_USER("Clicked");
  static uint8_t cnt1 = 0;//using static means that will read cnt = 0 only once
  lv_obj_t * btn1 = lv_event_get_target(event);
  lv_obj_t * label1 = lv_obj_get_child(btn1, 0);
  lv_label_set_text_fmt(label1, "Auto: %d", cnt1);
  cnt1++;
}

void lv_Auto_button(void)
{
    lv_obj_t * btn1 = lv_btn_create(lv_scr_act());
    lv_obj_set_size(btn1, 75,30);
    //lv_obj_add_event_cb(btn1, event_handler_1, LV_EVENT_ALL, NULL);
    lv_obj_add_event_cb(btn1, event_handler_1, LV_EVENT_CLICKED, NULL);
    lv_obj_align(btn1, LV_ALIGN_CENTER, 0, 80);
    
    lv_obj_t * label1 = lv_label_create(btn1);
    lv_label_set_text(label1, "Auto");
    lv_obj_center(label1);

    static lv_style_t style_label; //added to try and increase font size to montserrat_28                                            
    lv_style_init(&style_label); //added to try and increase font size to montserrat_28
    lv_style_set_text_font(&style_label, LV_STATE_DEFAULT, &lv_font_montserrat_28);//added to try and increase font size to montserrat_28
    
    lv_obj_t * label4 = lv_label_create(lv_scr_act());

    lv_obj_add_style(label4, LV_STATE_DEFAULT, &style_label);//added to try and increase font size to montserrat_28
    
    lv_label_set_text(label4, "DISTANCE");
    lv_obj_set_style_text_align(label4, LV_TEXT_ALIGN_CENTER, 0);
    lv_obj_align(label4, LV_ALIGN_CENTER, 0,-80);   
}*/

Screenshot and/or video

If possible, add screenshots and/or videos about the current state.

I use fonts defined in lv_conf.h so:
#define LV_THEME_DEFAULT_INCLUDE <stdint.h> /Include a header for the init. function/
#define LV_THEME_DEFAULT_INIT lv_theme_material_init
#define LV_THEME_DEFAULT_COLOR_PRIMARY lv_color_hex(0x01a2b1)
#define LV_THEME_DEFAULT_COLOR_SECONDARY lv_color_hex(0x44d1b6)
#define LV_THEME_DEFAULT_FLAG LV_THEME_MATERIAL_FLAG_LIGHT
#define LV_THEME_DEFAULT_FONT_SMALL &lv_font_montserrat_12
#define LV_THEME_DEFAULT_FONT_NORMAL &lv_font_montserrat_14
#define LV_THEME_DEFAULT_FONT_SUBTITLE &lv_font_montserrat_20
#define LV_THEME_DEFAULT_FONT_TITLE &lv_font_montserrat_24

and put 1 on type of font that want use still in lv:conf.h

#define LV_FONT_MONTSERRAT_8 0
#define LV_FONT_MONTSERRAT_10 0
#define LV_FONT_MONTSERRAT_12 1
#define LV_FONT_MONTSERRAT_14 1
#define LV_FONT_MONTSERRAT_16 0
#define LV_FONT_MONTSERRAT_18 0
#define LV_FONT_MONTSERRAT_20 1
#define LV_FONT_MONTSERRAT_22 0
#define LV_FONT_MONTSERRAT_24 1
#define LV_FONT_MONTSERRAT_26 0

and in code:
lv_obj_set_style_local_text_font(LabelToast, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, LV_THEME_DEFAULT_FONT_SMALL);

but i use still version 7.11…i dont know in 8.

Thanks for your help! I would really like to stay with v8.0.2.
I have put 1 to include LV_FONT_MONTSERRAT_28 in lv_conf.h

I think you’re using v7 APIs in v8. Try this:

    static lv_style_t style_label;                                
    lv_style_init(&style_label);
    lv_style_set_text_font(&style_label, &lv_font_montserrat_28); /* styles are just property-value pairs now, no state or part */

    lv_obj_add_style(label4, &style_label, LV_STATE_DEFAULT); /* fix order *'
1 Like

Hi embeddedt, fantastic that works a treat. Thanks a lot!