label() is used to draw text. Itβs a simplified version of paragraph() for rendering single-style text.
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
Set the text color.label().text("Red text").color(Color::RED)
label().text("Custom").color((255, 128, 0))
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")
Set the font weight.label().text("Bold").font_weight(FontWeight::BOLD)
label().text("Light").font_weight(FontWeight::LIGHT)
Set the font slant (italic).label().text("Italic").font_slant(FontSlant::ITALIC)
Set the font width (condensed, expanded, etc.).label().text("Condensed").font_width(FontWidth::CONDENSED)
Set text alignment.label().text("Centered").text_align(TextAlign::Center)
Set overflow behavior.label().text("Long text...").text_overflow(TextOverflow::Ellipsis)
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
Set the maximum number of lines.label()
.text("Long text that will wrap...")
.max_lines(2)
Set the line height multiplier.label()
.text("Multiple\nlines")
.line_height(1.5)
Set the width constraint.label().text("Fixed width").width(200.0)
Set the height constraint.label().text("Fixed height").height(50.0)
Set padding around the text.label().text("Padded").padding(10.0)
Set margin outside the label.label().text("Margin").margin(5.0)
Accessibility
Set the accessibility ID.
Set alternative text for screen readers.label().text("β").a11y_alt("Next button")
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))
- 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")