How is working the fonction lv_txt_get_size()

Hello I don’t found so much sample for this fonction, ATM Im trying

lv_point_t textSize;
lv_txt_get_size(&textSize, value, fonts, 0, 0, 60, LV_TEXT_FLAG_NONE);

And I have as result
For value = “18:15” > textSize.x = 9 and textSize.y = 80
For value = “ABCDEFGHIJKLMNOPQRSTUVWXYZ” > textSize.x = 15 and textSize.y = 400

The police is Montserrat_Bold_14
The text need 1 line for the first test and 3 lines for the second test (width is limited to 60)

Something is missing ?

Hi!

The results you’re getting (x=9, y=80) look suspicious — for Montserrat Bold 14, "18:15" on a single line should give something closer to x≈40px, y≈16px. The small x and large y suggest the font pointer might not be valid.

A few things to check:

1. Are you on LVGL v8 or v9? In v9 the function was renamed from lv_txt_get_sizelv_text_get_size. Make sure you’re calling the right one for your version.

2. How is fonts declared? The third parameter must be a const lv_font_t *. Can you share how fonts is defined? A common mistake is passing an array or an uninitialized pointer. The correct call looks like this:

lv_point_t textSize;
lv_text_get_size(&textSize, value, &lv_font_montserrat_14, 0, 0, 60, LV_TEXT_FLAG_NONE);
// textSize.x → width in pixels
// textSize.y → height in pixels

3. Parameters reminder:

  • letter_space = 0 → no extra spacing between letters
  • line_space = 0 → no extra spacing between lines
  • max_width = 60 → wraps text at 60px wide

With those settings and Montserrat 14, "ABCDEFGHIJKLMNOPQRSTUVWXYZ" wrapping to 3 lines should give roughly x≈58, y≈48.

Could you share how you declared and initialized fonts? That’s most likely where the issue is.

Thx a lot.
The problem was here, there is a “magic part” that switch fonts, and the code don’t generate error or crash but make magic number as result.

The fonction is working perfectly now.

And I m on Lvgl V8.

1 Like