Skip to content

Text

A piece of text, wrapping to its container's width in the inherited color and size.

ui.text

Basic

ui.text("Body text, composited on the CPU; only what changed is repainted.")
.selectable();

Sizes

ui.column().gap(4).children(|ui| {
ui.text("Small text").text_sm();
ui.text("Body text");
ui.text("Large text").text_size(20);
});

Weight and style

ui.row().gap(16).children(|ui| {
ui.text("Bold").font_bold();
ui.text("Italic").italic();
ui.text("Monospace").mono();
});

Color

let t = ui.theme();
ui.row().gap(16).children(|ui| {
ui.text("Muted").muted();
ui.text("Accent").text_color(t.accent);
ui.text("Danger").text_color(t.danger);
});

Wrap

ui.column().w(240).p(8).bordered().rounded_md().children(|ui| {
ui.text("Text wraps at the width of its container unless it is truncated.");
});

Truncate

ui.column().w(240).p(8).bordered().rounded_md().children(|ui| {
ui.text("One line that ends with an ellipsis when it does not fit.").truncate();
});

Alignment

ui.column().w(240).p(8).gap(4).bordered().rounded_md().children(|ui| {
ui.text("Start").w_fill();
ui.text("Center").w_fill().text_align(Align::Center);
ui.text("End").w_fill().text_align(Align::End);
});

CJK text

ui.column().gap(4).children(|ui| {
ui.text("正文文字,CPU 合成,只重绘改变的部分。");
ui.text("中文与 English 混排,标点「挤压」、(括号)与折行;一行放不下时自动换到下一行。");
});

Section heading

ui.heading("Heading");

Page title

ui.title("Title");

API reference

SettingDescription
.text_size(size: impl Length)The font size in logical pixels.
.text_sm()The theme's small font size.
.text_color(color: Color)The text color.
.muted()The secondary text color (Theme::muted_foreground).
.font_weight(weight: FontWeight)The weight of the strokes, from the font's own weights; a font without a heavier one gets wider strokes.
.font_thin()Weight 100 (FontWeight::THIN).
.font_extralight()Weight 200 (FontWeight::EXTRA_LIGHT).
.font_light()Weight 300 (FontWeight::LIGHT).
.font_normal()Weight 400 (FontWeight::NORMAL). Text is regular unless told otherwise; this undoes a heavier weight.
.font_medium()Weight 500 (FontWeight::MEDIUM).
.font_semibold()Weight 600 (FontWeight::SEMIBOLD).
.font_bold()Weight 700 (FontWeight::BOLD).
.font_extrabold()Weight 800 (FontWeight::EXTRA_BOLD).
.font_black()Weight 900 (FontWeight::BLACK).
.italic()Italic.
.mono()Monospaced font; short for .font(FontKind::Mono).
.font(font: FontKind)The font to draw with: a built-in kind, or a stack registered with Options::fonts.
.text_align(align: Align)The horizontal alignment of each line: start (default), center or end.
.selectable()Lets the user select and copy the text.
.truncate()One line, shortened with an ellipsis when it does not fit.
.w(width: impl Into<Sizing>)The width; a plain length is a fixed width.
.w_fill()Takes the remaining width.
Settings and results every widget has
.with(f: impl FnOnce(&mut W))Changes the widget being declared. This is how a widget defined outside CPUI gets settings that chain like the built-in ones, through a trait on its element.
.disabled(disabled: bool)Disables the widget when disabled is true.
.tooltip(text: impl Display)Shows text in a tooltip when the pointer rests on the widget.
.aria_label(text: impl Display)Names the widget for assistive technology without drawing anything.
.aria_role(role: accesskit::Role)What assistive technology is told the widget is, for a widget drawn on a canvas or built from others. A canvas given a role, a name or a value is described as an image unless given another role.
.aria_value(text: impl Display)The value assistive technology reads, as text: "3 of 5 stars", "Monday 4 March".
.aria_range(value: f64, min: f64, max: f64)A numeric value between min and max, for sliders, meters and progress. Assistive technology steps it with the arrow keys, so handle those while focused.
.self_start()Aligns to the cross-axis start (top in a stack), overriding the container.
.self_center()Centers this widget on the cross axis (vertically in a stack).
.self_end()Aligns this widget to the end of the cross axis (the bottom in a stack).
.self_stretch()Stretches this widget on the cross axis if it has no fixed size there.
.layout_priority(priority: f32)Sets the priority for keeping width when a row is too narrow.
.col_span(columns: u32)Spans columns columns of the grid.
.offset(x: impl crate::style::Length, y: impl crate::style::Length)Draws this widget x right and y down of its layout position, moving nothing else.
.justify_self_start()In a stack, aligns to the left.
.justify_self_center()In a stack, centers horizontally.
.justify_self_end()In a stack, aligns to the right.
.when(condition: bool, f: impl FnOnce(Self)Applies f when condition is true.
.when_some(value: Option<T>, f: impl FnOnce(Self, T)Applies f to the value when there is one.
.response()Places the widget and returns its full interaction result.
.clicked()Places the widget and returns whether it was clicked this frame.
.double_clicked()Places the widget and returns whether it was double-clicked this frame.
.secondary_clicked()Places the widget and returns whether it got a secondary (right) button press this frame.
.hovered()Places the widget and returns whether the pointer is over it.
.changed()Places the widget and returns whether its value changed this frame.
.submitted()Places the widget and returns Response::submitted.