# text

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

<a id="construction"></a>

## Construction

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

```rust
use refract::prelude::*;

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

<a id="typography"></a>

## 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`](/docs/field) for single-line or multiline editing.

<a id="overflow"></a>

## 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);
```

<a id="view-specific-reference"></a>

## 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`](#heading) | Sets the heading role and clamps the semantic level to 1–6. This does not choose a font size. |
| [`status`](#status) | Marks this element with the status role. On the web this maps to status semantics; visual styling remains separate. |
| [`label`](#label) | Sets the semantic label, including image alt text. Also available through `ViewExt` after styling; it labels the current outer element. |
| [`on_press`](#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`](#disabled) | Controls whether the element accepts interaction. The default is `false`; disabling a control does not remove it from layout. |
| [`font_size`](#font-size) | Sets a finite, nonnegative logical font size on the text or editable control. |
| [`font_weight`](#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`](#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`](#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`](#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`](#font-family) | Requests a font family on the text or editable control. Font availability and fallback belong to the platform adapter. |
| [`line_height`](#line-height) | Sets line height in logical pixels on the text or editable control. The value must be finite and nonnegative. |
| [`text_align`](#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`](#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`. |

<a id="heading"></a>

### `heading`

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

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

<a id="status"></a>

### `status`

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

```rust
fn status(self) -> Self
```

<a id="label"></a>

### `label`

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

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

<a id="on-press"></a>

### `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.

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

<a id="disabled"></a>

### `disabled`

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

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

<a id="font-size"></a>

### `font_size`

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

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

<a id="font-weight"></a>

### `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.

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

<a id="italic"></a>

### `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.

```rust
fn italic(self) -> Self
```

<a id="letter-spacing"></a>

### `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.

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

<a id="word-spacing"></a>

### `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.

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

<a id="font-family"></a>

### `font_family`

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

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

<a id="line-height"></a>

### `line_height`

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

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

<a id="text-align"></a>

### `text_align`

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

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

<a id="truncate"></a>

### `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`.

```rust
fn truncate(self) -> Self
```

<a id="common-modifiers"></a>

## 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](/docs/view-modifiers) for shared methods and modifier ordering. Focus bindings and callbacks preserve concrete builders; see [focus modifiers](/docs/view-modifiers#focus) and [Input & Focus](/docs/input-focus) for requests, observation, and indicators.

<a id="source"></a>

## 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`.
