Skip to main content
label() is used to draw text. It’s a simplified version of paragraph() for rendering single-style text.

Usage

use freya::prelude::*;

fn app() -> impl IntoElement {
    label()
        .text("Hello, world!")
        .font_size(16.0)
        .color(Color::BLACK)
}

String Conversion

You can use string literals and String values directly as elements:
// These are equivalent:
"Hello"
label().text("Hello")

Builder Methods

Text Content

text
impl Into<Cow<'static, str>>
Set the text content.
label().text("Hello")
label().text(String::from("Hello"))

Text Style

color
Color
Set the text color.
label().text("Red text").color(Color::RED)
label().text("Custom").color((255, 128, 0))
font_size
FontSize
Set the font size.
label().text("Large").font_size(24.0)
label().text("Small").font_size(12.0)
font_family
impl Into<Cow<'static, str>>
Set the font family.
label().text("Mono").font_family("Fira Code")
font_weight
FontWeight
Set the font weight.
label().text("Bold").font_weight(FontWeight::BOLD)
label().text("Light").font_weight(FontWeight::LIGHT)
font_slant
FontSlant
Set the font slant (italic).
label().text("Italic").font_slant(FontSlant::ITALIC)
font_width
FontWidth
Set the font width (condensed, expanded, etc.).
label().text("Condensed").font_width(FontWidth::CONDENSED)
text_align
TextAlign
Set text alignment.
label().text("Centered").text_align(TextAlign::Center)
text_overflow
TextOverflow
Set overflow behavior.
label().text("Long text...").text_overflow(TextOverflow::Ellipsis)
text_shadow
TextShadow
Add a text shadow.
label()
    .text("Shadowed")
    .text_shadow(TextShadow::new(
        (2.0, 2.0),
        5.0,
        Color::BLACK.with_opacity(0.5)
    ))

Text Layout

max_lines
impl Into<Option<usize>>
Set the maximum number of lines.
label()
    .text("Long text that will wrap...")
    .max_lines(2)
line_height
impl Into<Option<f32>>
Set the line height multiplier.
label()
    .text("Multiple\nlines")
    .line_height(1.5)

Layout

width
Size
Set the width constraint.
label().text("Fixed width").width(200.0)
height
Size
Set the height constraint.
label().text("Fixed height").height(50.0)
padding
Gaps
Set padding around the text.
label().text("Padded").padding(10.0)
margin
Gaps
Set margin outside the label.
label().text("Margin").margin(5.0)

Accessibility

a11y_id
AccessibilityId
Set the accessibility ID.
a11y_alt
impl Into<Box<str>>
Set alternative text for screen readers.
label().text("β†’").a11y_alt("Next button")

Events

on_press
EventHandler
Handle press/click events.
label()
    .text("Click me")
    .on_press(|_| println!("Clicked!"))
on_mouse_move
EventHandler<MouseEventData>
Handle mouse move events.

Examples

Basic Text

label()
    .text("Hello, Freya!")
    .font_size(24.0)
    .color(Color::BLACK)

Styled Heading

label()
    .text("Welcome")
    .font_size(32.0)
    .font_weight(FontWeight::BOLD)
    .color((50, 50, 255))

Truncated Text

label()
    .text("This is a very long text that will be truncated with an ellipsis")
    .width(200.0)
    .max_lines(1)
    .text_overflow(TextOverflow::Ellipsis)

Multi-line with Custom Line Height

label()
    .text("Line one\nLine two\nLine three")
    .line_height(1.8)
    .color(Color::GRAY)

Interactive Label

let mut clicked = use_state(|| false);

label()
    .text(if clicked() { "Clicked!" } else { "Click me" })
    .font_size(18.0)
    .color(if clicked() { Color::GREEN } else { Color::BLUE })
    .on_press(move |_| clicked.set(true))

Notes

  • Label automatically measures its content and sizes itself accordingly
  • For rich text with multiple styles, use paragraph() instead
  • Text color can be inherited from parent elements
  • Use string literals directly as a shorthand: "text" instead of label().text("text")

Build docs developers (and LLMs) love