先生成字庫文件,可以用官方在線工具
圖片轉換: https://littlevgl.com/image-to-c-array
字體轉換: https://littlevgl.com/ttf-font-to-c-array
也可以用下面的字體生成工具。
https://download.csdn.net/download/xinxiaoci/10905909
字體相關的結構體成員
static const uint8_t glyph_bitmap[]={……}; // 點陣數據
static const lv_font_glyph_dsc_t glyph_dsc[]={}; // 字體描述 {字體寬度,字體在點陣數中的起始索引值}
static const uint32_t unicode_list[] = {}; // 對應的Unicode編碼
填充結構體
lv_font_t basics_chinese_21 =
{
.unicode_first = 32, /* Unicode 最小編碼 */
.unicode_last = 65292, /* Unicode 最大編碼 */
.h_px = 21, /* 字體高度 */
.glyph_bitmap = glyph_bitmap, /* 字體點陣數據 */
.glyph_dsc = glyph_dsc, /* 字體點陣描述 寬度, */
.unicode_list = unicode_list, /* 字庫所包含的Unicode編碼列表 */
.get_bitmap = lv_font_get_bitmap_sparse, /* littlevgl 自帶函數 */
.get_width = lv_font_get_width_sparse, /* littlevgl 自帶函數 */
.bpp = 4, /* 抗鋸齒 */
.next_page = NULL, /* 字體擴展指針 */
};
在頭文件中調用 LV_FONT_DECLARE 宏聲明自己定義的字體
LV_FONT_DECLARE(basics_chinese_21);
調用方法,在需要用到漢字的地方,創建新的樣式 style ,在樣式中修改字體,然後基於新樣式創建對象。
void lv_chinese_fonts1(void)
{
/*Concatenate the fonts into one*/
// lv_font_add(&arial_cyrillic_20, &arial_ascii_20); 相同高度纔可以添加到一起
// lv_font_add(&arial_math_20, &arial_ascii_20);
/* 創建一個新的樣式,並且修改新樣式的文本字體 */
static lv_style_t style1;
lv_style_copy(&style1, &lv_style_plain);
style1.text.font = &basics_chinese_21; /* 設置自定義字體 */
/*Create a label and set new text*/
lv_obj_t * label = lv_label_create(lv_scr_act(), NULL); /* 創建標籤 */
lv_obj_set_pos(label, 10, 10); /* 設置相對位置 */
lv_label_set_style(label, &style1); /* 設置樣式 */
lv_label_set_text(label, "Hello World!\n 世界你好,我是littleVGL!"); /* 顯示漢字 */
}
在main函數中調用即可;
模擬器效果如下