Span Group (lv_spangroup)
Rich text built from spans, each with its own font, color and size.
Overview
The Span Group displays rich text built from multiple spans, each with its own text, font, color and size. Spans can be added or removed at runtime.
Learn more in the LVGL Open documentation.
/**
* @file lv_example_spangroup.c
*/
#include "../lv_examples.h"
/**
* @title Span group
* @brief Rich text built from spans with different fonts, colours and sizes.
*
* Each `<lv_spangroup-span>` element carries its own `text` and a named
* `style`, so one paragraph mixes colours and sizes. `overflow="ellipsis"`
* and `max_lines` clip the block gracefully, and `indent` offsets the first
* line.
*/
void lv_example_spangroup(void)
{
LV_FONT_DECLARE(font_large);
static lv_style_t style_normal;
static lv_style_t style_accent;
static lv_style_t style_big;
static lv_style_t style_panel;
static bool inited = false;
if (!inited) {
/*Init all styles*/
lv_style_init(&style_normal);
lv_style_init(&style_accent);
lv_style_init(&style_big);
lv_style_init(&style_panel);
lv_style_set_text_color(&style_normal, lv_color_hex(0xe5e7eb));
lv_style_set_text_color(&style_accent, lv_color_hex(0x3b82f6));
lv_style_set_text_color(&style_big, lv_color_hex(0xf59e0b));
lv_style_set_text_font(&style_big, &font_large);
lv_style_set_bg_color(&style_panel, lv_color_hex(0x0f172a));
lv_style_set_bg_opa(&style_panel, (255 * 100 / 100));
lv_style_set_radius(&style_panel, 8);
lv_style_set_pad_all(&style_panel, 12);
inited = true;
}
lv_obj_t * screen = lv_screen_active();
lv_obj_t * lv_spangroup_0 = lv_spangroup_create(screen);
lv_obj_set_size(lv_spangroup_0, 260, LV_SIZE_CONTENT);
lv_spangroup_set_overflow(lv_spangroup_0, LV_SPAN_OVERFLOW_ELLIPSIS);
lv_spangroup_set_max_lines(lv_spangroup_0, 3);
lv_spangroup_set_indent(lv_spangroup_0, 16);
lv_obj_set_align(lv_spangroup_0, LV_ALIGN_CENTER);
lv_obj_add_style(lv_spangroup_0, &style_panel, 0);
lv_span_t * lv_spangroup_span_0 = lv_spangroup_add_span(lv_spangroup_0);
lv_spangroup_set_span_text(lv_spangroup_0, lv_spangroup_span_0, "LVGL ");
lv_spangroup_set_span_style(lv_spangroup_0, lv_spangroup_span_0, &style_normal);
lv_span_t * lv_spangroup_span_1 = lv_spangroup_add_span(lv_spangroup_0);
lv_spangroup_set_span_text(lv_spangroup_0, lv_spangroup_span_1, "Pro");
lv_spangroup_set_span_style(lv_spangroup_0, lv_spangroup_span_1, &style_big);
lv_span_t * lv_spangroup_span_2 = lv_spangroup_add_span(lv_spangroup_0);
lv_spangroup_set_span_text(lv_spangroup_0, lv_spangroup_span_2, " lets you mix ");
lv_spangroup_set_span_style(lv_spangroup_0, lv_spangroup_span_2, &style_normal);
lv_span_t * lv_spangroup_span_3 = lv_spangroup_add_span(lv_spangroup_0);
lv_spangroup_set_span_text(lv_spangroup_0, lv_spangroup_span_3, "colours");
lv_spangroup_set_span_style(lv_spangroup_0, lv_spangroup_span_3, &style_accent);
lv_span_t * lv_spangroup_span_4 = lv_spangroup_add_span(lv_spangroup_0);
lv_spangroup_set_span_text(lv_spangroup_0, lv_spangroup_span_4, " and sizes in a single block of text.");
lv_spangroup_set_span_style(lv_spangroup_0, lv_spangroup_span_4, &style_normal);
}
<!--
@title Span group
@brief Rich text built from spans with different fonts, colours and sizes.
Each `<lv_spangroup-span>` element carries its own `text` and a named
`style`, so one paragraph mixes colours and sizes. `overflow="ellipsis"`
and `max_lines` clip the block gracefully, and `indent` offsets the first
line.
-->
<screen>
<styles>
<style name="style_normal" text_color="0xe5e7eb" />
<style name="style_accent" text_color="0x3b82f6" />
<style name="style_big" text_color="0xf59e0b" text_font="font_large" />
<style name="style_panel" bg_color="0x0f172a" bg_opa="100%" radius="8" pad_all="12" />
</styles>
<view>
<lv_spangroup width="260" height="content" overflow="ellipsis" max_lines="3" indent="16" align="center">
<style name="style_panel" />
<lv_spangroup-span text="LVGL " style="style_normal" />
<lv_spangroup-span text="Pro" style="style_big" />
<lv_spangroup-span text=" lets you mix " style="style_normal" />
<lv_spangroup-span text="colours" style="style_accent" />
<lv_spangroup-span text=" and sizes in a single block of text." style="style_normal" />
</lv_spangroup>
</view>
</screen>
Enumerations
lv_span_overflow — How to handle overflowing text
| Value | Description |
|---|---|
clip | Clip at boundary |
ellipsis | Replace overflowing words with ... |
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 block and its background: text and background properties. Style individual spans via each span's own style. |
Properties below are the widget's XML <api>; see API for how properties, parameters and elements work.
| Property | Type | Description |
|---|---|---|
overflow | enum:lv_span_overflow | Set overflow mode |
max_lines | int | Set max lines |
indent | int | Set first line indent (px) |
<lv_spangroup-span>
Add a styled text span
access add · returns lv_span
| Name | Kind | Type | Description |
|---|---|---|---|
text | prop | string | Set span text |
style | prop | style | Set span style |
bind_text | prop | subject | Bind text to a subject |
bind_text-fmt | prop | string | Format string (printf style) (default: NULL) |
Last updated on