How do I change the button font for LV_SYMBOL

Description

I want to change the btn font,LV_SYMBOL is displayed by calling the LV_obj_set_style_local_value_str interface

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

VS2017

What LVGL version are you using?

V7.6

What do you want to achieve?

LV_SYMBOL is displayed by calling the LV_obj_set_style_local_value_str interface

What have you tried so far?

lv_obj_add_style

Code to reproduce

void usr_test_demo(void)
{
    #define LV_SYMBOL_MESG             "\xef\x80\xa2" /*61474, 0xF022*/

    static lv_style_t style_font_tYaHeiB18;
    LV_FONT_DECLARE(lv_Font_YaHeiB_18)
    lv_style_init(&style_font_tYaHeiB18);
    lv_style_set_text_font(&style_font_tYaHeiB18, LV_STATE_DEFAULT, &lv_Font_YaHeiB_18);

    /* btn and label */
    lv_obj_t* btn = lv_btn_create(lv_scr_act(), NULL);
    lv_obj_align(btn, NULL, LV_ALIGN_CENTER, 0, 0);
    //lv_obj_set_style_local_value_str(btn, LV_BTN_PART_MAIN, LV_STATE_DEFAULT, "12345");
    lv_obj_t* label;
    label = lv_label_create(btn, NULL);
    lv_obj_add_style(label, LV_LABEL_PART_MAIN, &style_font_tYaHeiB18);
    lv_label_set_text(label, LV_SYMBOL_MESG"12345");

    /* btn style */
    //Qt: LV_SYMBOL_MESG don't exe
    static lv_style_t style_ripple;
    lv_style_init(&style_ripple);
    //lv_style_set_value_str(&style_ripple, LV_STATE_DEFAULT, LV_SYMBOL_MESG);
    //lv_style_set_value_font(&style_ripple, LV_STATE_DEFAULT, &style_font_tYaHeiB18);    //exe error
    lv_style_set_text_font(&style_ripple, LV_STATE_DEFAULT, &style_font_tYaHeiB18);

    lv_obj_t* btn1 = lv_btn_create(lv_scr_act(), NULL);
    lv_obj_align(btn1, btn, LV_ALIGN_OUT_BOTTOM_MID, 0, 50);
    //lv_obj_add_style(btn1, LV_STATE_DEFAULT, &style_font_tYaHeiB18);
    lv_obj_add_style(btn1, LV_STATE_DEFAULT, &style_ripple);
    
    //lv_obj_set_style_local_value_font(btn1, LV_BTN_PART_MAIN, LV_STATE_DEFAULT, &style_font_tYaHeiB18); //exe error

    lv_obj_set_style_local_value_str(btn1, LV_BTN_PART_MAIN, LV_STATE_DEFAULT, LV_SYMBOL_MESG"12345");

    /* primitive operation */
    lv_obj_t* btn2 = lv_btn_create(lv_scr_act(), NULL);
    lv_obj_align(btn2, btn1, LV_ALIGN_OUT_BOTTOM_MID, 0, 50);
    lv_obj_set_style_local_value_str(btn2, LV_BTN_PART_MAIN, LV_STATE_DEFAULT, LV_SYMBOL_WIFI"12345");
}

Screenshot and/or video

微信截图_20201014130807

Maybe you can try:
lv_obj_add_style(btn1, LV_BTN_PART_MAIN, &style_ripple);

  • I’ve tried this configuration before, but it doesn’t work.
  • I did it again

There is an error in this line of code, I guess it should be like this:
lv_obj_add_style(label, LV_LABEL_PART_MAIN, &style_font_tYaHeiB18);
After modification:
lv_obj_add_style(label, LV_LABEL_PART_MAIN, &lv_Font_YaHeiB_18);

If modified so, the program runs the exception directly.
Button matching with label display is no problem, LV_SYMBOL Icon display can be achieved.
However, lv_obj_set_style_local_value_str cannot be used to display LV_SYMBOL. It feels like the problem is that the loading style font failed, because LV_SYMBOL can be used to display successfully (third key graph).

That won’t work, because you need to add a style to the object, not a font. A compiler with appropriate warnings enabled should flag that as an error.

You need to set value_font, not text_font, if you want to change the font used by value_str.

I got it, tks, I made a slight mistake