1
0
mirror of https://github.com/lvgl/lvgl.git synced 2025-01-14 06:42:58 +08:00
lvgl/examples/libs/tiny_ttf/lv_example_tiny_ttf_3.c
Gabor Kiss-Vamosi 90a7e3e98f feat(tiny_ttf): add lv_font_* as paramater to lv_tiny_ttf_create
This way if a font is used by styles or widgets it can be easily reloaded without updating the font pointer on all places
2023-11-13 14:47:13 +01:00

56 lines
1.8 KiB
C

#include "../../lv_examples.h"
#if LV_USE_TINY_TTF && LV_BUILD_EXAMPLES && LV_USE_OBSERVER
static void font_size_observer_cb(lv_subject_t * subject, lv_observer_t * observer);
static lv_subject_t subject_font;
static lv_font_t font;
/**
* Change font size with Tiny_TTF
*/
void lv_example_tiny_ttf_3(void)
{
extern const uint8_t ubuntu_font[];
extern const int ubuntu_font_size;
lv_subject_init_int(&subject_font, 25);
/*Create style with the new font*/
static lv_style_t style;
lv_style_init(&style);
lv_result_t res = lv_tiny_ttf_create_data(&font, ubuntu_font, ubuntu_font_size, 25);
if(res == LV_RESULT_INVALID) return;
lv_style_set_text_font(&style, &font);
lv_style_set_text_align(&style, LV_TEXT_ALIGN_CENTER);
lv_obj_t * slider = lv_slider_create(lv_screen_active());
lv_obj_center(slider);
lv_slider_set_range(slider, 5, 50);
lv_obj_align(slider, LV_ALIGN_BOTTOM_MID, 0, -50);
lv_slider_bind_value(slider, &subject_font);
lv_obj_t * slider_label = lv_label_create(lv_screen_active());
lv_obj_align_to(slider_label, slider, LV_ALIGN_OUT_BOTTOM_MID, 0, 10);
lv_label_bind_text(slider_label, &subject_font, "%d");
/*Create a label with the new style*/
lv_obj_t * label = lv_label_create(lv_screen_active());
lv_obj_add_style(label, &style, 0);
lv_obj_set_size(label, LV_SIZE_CONTENT, LV_SIZE_CONTENT);
lv_label_set_text(label, "Hello world!");
lv_obj_center(label);
lv_subject_add_observer(&subject_font, font_size_observer_cb, &style);
}
static void font_size_observer_cb(lv_subject_t * subject, lv_observer_t * observer)
{
lv_style_t * style = observer->user_data;
int32_t size = lv_subject_get_int(subject);
lv_tiny_ttf_set_size(&font, size);
lv_obj_report_style_change(style);
}
#endif