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::*.
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.
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.
| Method | Purpose |
|---|---|
heading | Sets the heading role and clamps the semantic level to 1–6. This does not choose a font size. |
status | Marks this element with the status role. On the web this maps to status semantics; visual styling remains separate. |
label | Sets the semantic label, including image alt text. Also available through ViewExt after styling; it labels the current outer element. |
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. |
disabled | Controls whether the element accepts interaction. The default is false; disabling a control does not remove it from layout. |
font_size | Sets a finite, nonnegative logical font size on the text or editable control. |
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. |
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. |
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. |
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. |
font_family | Requests a font family on the text or editable control. Font availability and fallback belong to the platform adapter. |
line_height | Sets line height in logical pixels on the text or editable control. The value must be finite and nonnegative. |
text_align | Aligns text lines within their available width. Use Start, Center, or End. This is separate from aligning a child inside a stack. |
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. |
heading
Sets the heading role and clamps the semantic level to 1–6. This does not choose a font size.
fn heading(self, level: u8) -> Selfstatus
Marks this element with the status role. On the web this maps to status semantics; visual styling remains separate.
fn status(self) -> Selflabel
Sets the semantic label, including image alt text. Also available through ViewExt after styling; it labels the current outer element.
fn label(self, label: impl Into<String>) -> Selfon_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.
fn on_press(self, action: impl Fn(&mut ActionContext<'_>) + 'static) -> Selfdisabled
Controls whether the element accepts interaction. The default is false; disabling a control does not remove it from layout.
fn disabled(self, value: bool) -> Selffont_size
Sets a finite, nonnegative logical font size on the text or editable control.
fn font_size(self, value: f32) -> Selffont_weight
Requests a numeric font weight, such as 400 for regular or 700 for bold. The selected face depends on the host's fonts.
fn font_weight(self, value: u16) -> Selfitalic
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.
fn italic(self) -> Selfletter_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.
fn letter_spacing(self, value: f32) -> Selfword_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.
fn word_spacing(self, value: f32) -> Selffont_family
Requests a font family on the text or editable control. Font availability and fallback belong to the platform adapter.
fn font_family(self, value: impl Into<String>) -> Selfline_height
Sets line height in logical pixels on the text or editable control. The value must be finite and nonnegative.
fn line_height(self, value: f32) -> Selftext_align
Aligns text lines within their available width. Use Start, Center, or End. This is separate from aligning a child inside a stack.
fn text_align(self, value: Alignment) -> Selftruncate
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.
fn truncate(self) -> SelfCommon 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.