Documentation / Reference

text

Display a string with typography, wrapping, and semantic labels.

Construction

Use text(value: impl Into<String>) -> Text. Import constructors and common modifiers from refract::prelude::*.

text.rs
use refract::prelude::*;

let title = text("Your library")
    .heading(1)
    .font_size(28.0)
    .font_weight(600)
    .foreground(Color::BLUE)
    .padding(16.0);

Typography and layout

The text node owns its string. Read a binding inside a reactive view and pass its current value to text to update the label. The platform adapter measures and renders the string.

Apply typography to the text before padding or frame introduces a box. Constrain the width to control wrapping. Use field for single-line or multiline editing.

Single-line overflow

Use truncate() with a bounded width to display a single line with an ellipsis on web and native. Spaces, tabs, and line breaks collapse into spaces; the original string and semantic label remain unchanged. Resizing reveals or truncates text to fit the new width.

Configure truncation and the text's width before adding padding or frame. If even the ellipsis cannot fit, it is clipped to the available width.

Rust
let title = text("A long title that may not fit in the sidebar")
    .width(180.0)
    .truncate()
    .padding(8.0);

View-specific reference

text returns Text. Typography, heading/status semantics, labels, and activation preserve Text. Text does not expose child composition or stack alignment.

MethodPurpose
headingSets the heading role and clamps the semantic level to 1–6. This does not choose a font size.
statusMarks this element with the status role. On the web this maps to status semantics; visual styling remains separate.
labelSets the semantic label, including image alt text. Also available through ViewExt after styling; it labels the current outer element.
on_pressAdds pointer/keyboard activation to a box, text, or image without changing its primitive kind. Callbacks compose in registration order and preserve other event handlers. Preserves the concrete primitive or scroll builder; the ViewExt fallback returns Modified for composed views. Structural modifier order defines the hit area. Targets default to button semantics unless a role is already set. On the web, their text is non-selectable so repeated clicks do not highlight labels.
disabledControls whether the element accepts interaction. The default is false; disabling a control does not remove it from layout.
font_sizeSets a finite, nonnegative logical font size on the text or editable control.
font_weightRequests a numeric font weight, such as 400 for regular or 700 for bold. The selected face depends on the host's fonts.
italicRequests an italic face. When unavailable, the host synthesizes a slant. Available on Text and Field, including editable text and placeholders. Apply before padding or frame introduces a wrapper.
letter_spacingAdds spacing between letters in logical pixels. Values must be finite; negative values tighten text and zero restores normal spacing. Available on Text and Field. Changing spacing updates text measurement and wrapping.
word_spacingAdds spacing between words in logical pixels. Values must be finite; negative values tighten spacing and zero restores normal spacing. Available on Text and Field, including placeholders.
font_familyRequests a font family on the text or editable control. Font availability and fallback belong to the platform adapter.
line_heightSets line height in logical pixels on the text or editable control. The value must be finite and nonnegative.
text_alignAligns text lines within their available width. Use Start, Center, or End. This is separate from aligning a child inside a stack.
truncateDisplays a single line with an ellipsis when text exceeds its bounded width, on both web and native. Collapses whitespace and preserves the full underlying string and semantic label. Preserves Text; apply before wrapper modifiers such as padding or frame.

heading

Sets the heading role and clamps the semantic level to 1–6. This does not choose a font size.

Signature
fn heading(self, level: u8) -> Self

status

Marks this element with the status role. On the web this maps to status semantics; visual styling remains separate.

Signature
fn status(self) -> Self

label

Sets the semantic label, including image alt text. Also available through ViewExt after styling; it labels the current outer element.

Signature
fn label(self, label: impl Into<String>) -> Self

on_press

Adds pointer/keyboard activation to a box, text, or image without changing its primitive kind. Callbacks compose in registration order and preserve other event handlers. Preserves the concrete primitive or scroll builder; the ViewExt fallback returns Modified for composed views. Structural modifier order defines the hit area. Targets default to button semantics unless a role is already set. On the web, their text is non-selectable so repeated clicks do not highlight labels.

Signature
fn on_press(self, action: impl Fn(&mut ActionContext<'_>) + 'static) -> Self

disabled

Controls whether the element accepts interaction. The default is false; disabling a control does not remove it from layout.

Signature
fn disabled(self, value: bool) -> Self

font_size

Sets a finite, nonnegative logical font size on the text or editable control.

Signature
fn font_size(self, value: f32) -> Self

font_weight

Requests a numeric font weight, such as 400 for regular or 700 for bold. The selected face depends on the host's fonts.

Signature
fn font_weight(self, value: u16) -> Self

italic

Requests an italic face. When unavailable, the host synthesizes a slant. Available on Text and Field, including editable text and placeholders. Apply before padding or frame introduces a wrapper.

Signature
fn italic(self) -> Self

letter_spacing

Adds spacing between letters in logical pixels. Values must be finite; negative values tighten text and zero restores normal spacing. Available on Text and Field. Changing spacing updates text measurement and wrapping.

Signature
fn letter_spacing(self, value: f32) -> Self

word_spacing

Adds spacing between words in logical pixels. Values must be finite; negative values tighten spacing and zero restores normal spacing. Available on Text and Field, including placeholders.

Signature
fn word_spacing(self, value: f32) -> Self

font_family

Requests a font family on the text or editable control. Font availability and fallback belong to the platform adapter.

Signature
fn font_family(self, value: impl Into<String>) -> Self

line_height

Sets line height in logical pixels on the text or editable control. The value must be finite and nonnegative.

Signature
fn line_height(self, value: f32) -> Self

text_align

Aligns text lines within their available width. Use Start, Center, or End. This is separate from aligning a child inside a stack.

Signature
fn text_align(self, value: Alignment) -> Self

truncate

Displays a single line with an ellipsis when text exceeds its bounded width, on both web and native. Collapses whitespace and preserves the full underlying string and semantic label. Preserves Text; apply before wrapper modifiers such as padding or frame.

Signature
fn truncate(self) -> Self

Common view modifiers

Dimensions, paint, labels, and disabled state preserve each concrete builder type, so their builder methods can be interleaved. padding and frame return wrappers; configure the inner element before adding those. ViewExt supplies the shared API for composed views.

See View Modifiers for shared methods and modifier ordering. Focus bindings and callbacks preserve concrete builders; see focus modifiers and Input & Focus for requests, observation, and indicators.

Source reference

The implementation is in crates/core/src/view/primitives.rs. Primitive and layout builders are in crates/core/src/view/primitives.rs. Shared modifiers are in crates/core/src/view/modifiers.rs. Style values and validation are in crates/core/src/style.rs.