Skip to main content

LabelBuilder

Trait LabelBuilder 

Source
pub trait LabelBuilder: Sized {
Show 17 methods // Required methods fn single_line(self) -> Self; fn max_lines(self, lines: u32) -> Self; fn font(self, f: Font) -> Self; fn font_scale(self, scale: f64) -> Self; fn weight(self, w: FontWeight) -> Self; fn bold(self) -> Self; fn secondary(self) -> Self; fn italic(self) -> Self; fn tabular(self) -> Self; fn monospace(self) -> Self; fn runs(self, runs: Vec<TextRun>) -> Self; fn runs_from(self, b: TextBuilder) -> Self; fn align(self, align: TextAlign) -> Self; fn markdown(self) -> Self; fn on_link(self, f: impl Fn(&str) + 'static) -> Self; fn color<M>(self, c: impl IntoReactive<Color, M>) -> Self; fn preset(self, preset: &TextPreset) -> Self;
}
Expand description

Builder methods of Label, reachable through a Decorated (docs/api-style.md “Typed builders and erasure”): generic modifiers and typed ones chain in any order.

Required Methods§

Source

fn single_line(self) -> Self

Keep plain text on one line. UIKit, Android, GTK, AppKit and DOM truncate with an ellipsis.

Source

fn max_lines(self, lines: u32) -> Self

Limit a plain label to this many lines, truncating the last line on native Apple backends. Zero means unlimited. Rebuild the label to change its line limit.

Source

fn font(self, f: Font) -> Self

The semantic text style (Font::Title, Font::Footnote, …) or a custom Font::System(pt). Backends render it with the platform’s native style + accessibility text scaling.

Source

fn font_scale(self, scale: f64) -> Self

Scale the resolved native font, preserving its semantic style and accessibility size. Applies to the label’s base font; explicit styled runs retain their own descriptors.

Source

fn weight(self, w: FontWeight) -> Self

Override the font weight (e.g. FontWeight::Semibold). See also Label::bold.

Source

fn bold(self) -> Self

Shorthand for .weight(FontWeight::Bold).

Source

fn secondary(self) -> Self

De-emphasize the text: the platform’s secondary label color, whatever that is here (secondaryLabelColor on Apple, ?android:textColorSecondary, a dim label on GTK).

Semantic rather than a literal grey, for the reason a literal grey cannot solve: one that reads well on white is wrong on black, so an app that hard-codes it gets a label that is either washed out or nearly invisible in the other appearance. This is what an empty state’s “nothing selected”, a hint under a field, or a caption should wear.

Source

fn italic(self) -> Self

Render the text italic (slanted).

Source

fn tabular(self) -> Self

Ask for tabular (monospaced) figures, so a changing number stops changing width.

Pair it with Decorate::reserving for a readout beside a slider: reserving stops the box resizing when the digit count changes, tabular stops the digits shifting inside it because 1 is narrower than 8. See day_spec::FontSpec::tabular.

Source

fn monospace(self) -> Self

Ask for the platform’s monospaced face at this style’s size, which is what inline code wants.

Source

fn runs(self, runs: Vec<TextRun>) -> Self

Style spans within this label’s text (docs/text-runs.md): one wrapping paragraph with emphasis, color, code or a link inside it, rather than several labels in a row.

Ranges are byte offsets into the label’s text, ascending and non-overlapping; text not covered by a run draws with the label’s own font. Invalid runs are REJECTED at build time with a warning and the label renders plain, because the alternative is eight different wrong renderings (and a panic on the backends that slice str).

TextBuilder is the ergonomic way in; this is the direct one.

Source

fn runs_from(self, b: TextBuilder) -> Self

Take both the text and its runs from a TextBuilder, replacing whatever text the label was built with. This is the intended entry point: the builder guarantees the ranges match the string, which is the invariant hand-written runs get wrong.

Source

fn align(self, align: TextAlign) -> Self

Read the label’s text as inline Markdown (docs/markdown.md): **bold**, *italic*, `code`, ~~strike~~ and [text](url) become styled runs, and the markers themselves are stripped.

The parse happens at run time, on every change, so it works on a translated string chosen from the locale bundle, a value off the network, or text a user is typing, none of which a compile-time macro can see. The cost is a parse per update of a string that is a label’s worth of text.

ⓘ
label(tr("release-note")).markdown()
label(move || draft.get()).markdown()   // live as the user types

Unrecognized markup stays literal, so a half-typed ** reads as two asterisks rather than flickering. Block constructs (headings, lists, quotes) are not parsed: they are layout, which is column/form/list. Center (or trail) this label’s lines within its own width, for the short wrapped block a welcome screen or an empty state uses. Only observable on a label that wraps, since a single line already fills its box.

Source

fn markdown(self) -> Self

Handle a tapped link run yourself instead of opening its target.

Without this, a #route target navigates in-app and other targets open in the platform’s default handler, the same as the link piece. This handler overrides both behaviors; call open_link to delegate targets you do not handle back to the default.

Activation is Cap::TextLinks, which is narrower than run rendering: on a backend without it the link still draws, and nothing calls this (docs/text-runs.md).

Source

fn color<M>(self, c: impl IntoReactive<Color, M>) -> Self

The text color: a constant, a Signal<Color>, or a Fn() -> Color. A reactive source recolors the native label when it changes (theme systems ride this).

Source

fn preset(self, preset: &TextPreset) -> Self

Apply a named TextPreset: the font, weight, scale, emphasis and color an app defined once (label(t).preset(&CAPTION)).

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§

Source§

impl LabelBuilder for Label

Source§

impl<DecoratedInner: LabelBuilder + Piece> LabelBuilder for Decorated<DecoratedInner>

Day API ↩ Guides· daybrite.dev