Label (lv_label)
The widget used to display text, with wrapping and long-text modes.
Overview
The Label is the widget used to display text. It supports line wrapping, several long-text modes (scroll, dots, clip), per-word recoloring, and binding its text to a subject with printf-style formatting.
Learn more in the LVGL Open documentation.
/**
* @file lv_example_label.c
*/
#include "../lv_examples.h"
/**
* @title Label
* @brief Recoloring, a long-text mode and a value bound to a subject.
*
* Three labels. The first uses `recolor="true"` so `#hex text#` spans change
* colour inline. The second is given a fixed width with
* `long_mode="scroll_circular"` so the over-long text scrolls continuously. The
* last uses `bind_text` with a `bind_text-fmt` printf string to track
* `subject_value` live.
*/
void lv_example_label(void)
{
static lv_style_t style_box;
static lv_subject_t subject_value;
static bool inited = false;
if (!inited) {
/*Init all styles*/
lv_style_init(&style_box);
lv_style_set_bg_color(&style_box, lv_color_hex(0x1f2937));
lv_style_set_bg_opa(&style_box, (255 * 100 / 100));
lv_style_set_radius(&style_box, 8);
lv_style_set_pad_all(&style_box, 10);
lv_style_set_text_color(&style_box, lv_color_hex(0xe5e7eb));
lv_style_set_width(&style_box, 260);
lv_subject_init_int(&subject_value, 50);
lv_subject_set_min_value_int(&subject_value, 0);
lv_subject_set_max_value_int(&subject_value, 100);
inited = true;
}
lv_obj_t * screen = lv_screen_active();
lv_obj_set_flex_flow(screen, LV_FLEX_FLOW_COLUMN);
lv_obj_set_style_pad_row(screen, 12, 0);
lv_obj_set_style_flex_cross_place(screen, LV_FLEX_ALIGN_CENTER, 0);
lv_obj_set_style_flex_main_place(screen, LV_FLEX_ALIGN_CENTER, 0);
lv_obj_set_style_flex_track_place(screen, LV_FLEX_ALIGN_CENTER, 0);
/* Per-word recoloring */
lv_obj_t * lv_label_0 = lv_label_create(screen);
lv_label_set_recolor(lv_label_0, true);
lv_label_set_text(lv_label_0, "Status: #16a34a OK# / #dc2626 ERROR#");
lv_obj_add_style(lv_label_0, &style_box, 0);
/* Fixed width + circular scroll for overflow */
lv_obj_t * lv_label_1 = lv_label_create(screen);
lv_obj_set_width(lv_label_1, 200);
lv_label_set_long_mode(lv_label_1, LV_LABEL_LONG_MODE_SCROLL_CIRCULAR);
lv_label_set_text(lv_label_1, "This text is too long to fit so it scrolls around.");
lv_obj_add_style(lv_label_1, &style_box, 0);
/* Live value bound to a subject */
lv_obj_t * lv_label_2 = lv_label_create(screen);
lv_label_bind_text(lv_label_2, &subject_value, "Bound value: %d %%");
lv_obj_add_style(lv_label_2, &style_box, 0);
}
<!--
@title Label
@brief Recoloring, a long-text mode and a value bound to a subject.
Three labels. The first uses `recolor="true"` so `#hex text#` spans change
colour inline. The second is given a fixed width with
`long_mode="scroll_circular"` so the over-long text scrolls continuously. The
last uses `bind_text` with a `bind_text-fmt` printf string to track
`subject_value` live.
-->
<screen>
<styles>
<style
name="style_box"
bg_color="0x1f2937"
bg_opa="100%"
radius="8"
pad_all="10"
text_color="0xe5e7eb"
width="260"
/>
</styles>
<view
flex_flow="column"
style_pad_row="12"
style_flex_cross_place="center"
style_flex_main_place="center"
style_flex_track_place="center"
>
<!-- 💡 The scrolling label's text is wider than its fixed width, so it loops. -->
<!-- Per-word recoloring -->
<lv_label recolor="true" text="Status: #16a34a OK# / #dc2626 ERROR#">
<style name="style_box" />
</lv_label>
<!-- Fixed width + circular scroll for overflow -->
<lv_label width="200" long_mode="scroll_circular" text="This text is too long to fit so it scrolls around.">
<style name="style_box" />
</lv_label>
<!-- Live value bound to a subject -->
<lv_label bind_text="subject_value" bind_text-fmt="Bound value: %d %%">
<style name="style_box" />
</lv_label>
</view>
</screen>
Enumerations
lv_label_long_mode — How to handle text that doesn’t fit
| Value | Description |
|---|---|
wrap | Wrap to next line |
dots | Show dots (…) at the end |
scroll | Scroll horizontally |
scroll_circular | Scroll continuously in a loop |
clip | Clip at the boundary |
Properties
Parts
Style parts of the widget with local style (e.g. style_bg_color-knob="0xff0000") or with style sheets (e.g. <style name="style_knob" selector="knob">). See Styles to learn more.
| Part | Description |
|---|---|
main | Style the text and its area: text properties (text_color, text_font, text_letter_space, text_line_space, text_decor, text_align) plus background/padding if needed. |
selected | Style the highlighted character range (text_selection_start/end). Only text_color and bg_color are used. |
Properties below are the widget's XML <api>; see API for how properties, parameters and elements work.
| Property | Type | Description |
|---|---|---|
text | string | Set label text |
translation_tag | string | Set translation tag |
long_mode | enum:lv_label_long_mode | Choose overflow mode |
recolor | bool | Allow recoloring words like: A #ff0000 red# word. |
text_selection_start | int | Beginning of text highlight |
text_selection_end | int | End of text highlight |
max_lines | int | Show at most this many lines. Works with long_mode wrap or dots |
bind_text | subject | Bind text to a subject |
bind_text-fmt | string | Format string for bound text (printf style) (default: NULL) |
Last updated on