Skip to content

Text

Text renders a string as body, label, or title content using the current theme.

Column {
Text("Project settings", TextRole::Title),
Text("Changes are saved automatically."),
Text("CONNECTED", TextRole::Label),
}.With(Spacing(6.0F))

Construct Text from StringResource, std::string, std::string_view, a string literal, or a printable State<T>. Text::Format replaces ordered {} placeholders and accepts either a string view or localized StringResource.

Text::Format("{} of {} files", completed, total)
Text::Format(app::strings::welcome, user_name)

Escape literal braces as {{ and }}. A mismatch between placeholders and arguments throws std::invalid_argument.

.Style(TextStyle) supplies the complete base font, foreground color, and decoration. Use .Shaping(...) separately for direction and locale. Use FontSize or Foreground for focused overrides that should compose with the theme-resolved role style.

Text("Warning")
.Style(TextStyle{
.font = Font::System(15.0F).WithWeight(FontWeight::SemiBold),
.foreground = Color::Rgb(180, 40, 40),
})

TextRole::Body, Label, and Title select defaults from the current typography and color scheme.

Text exposes text semantics automatically. Wrap a subtree in SelectionArea to enable shared pointer or touch text selection and the HuxerUI selection menu.

  • Constructors: Text(StringResource|string|string_view|const char*, TextRole = Body) and Text(State<T>, TextRole = Body).
  • Static factories: Text::Format(...), including role-first and resource overloads.
  • Fluent method: .Style(TextStyle).
  • Common modifiers: Foreground, FontSize, Padding, Frame, Semantics.

See Text and input API for fonts, layout options, metrics, runs, and measurement.

#include <huxerui/huxerui.h>
#include <string>
using namespace huxerui;
[[huxerui::composable]]
View LinkedSummary() {
auto target = UseState<std::string>("");
const AttributedText summary{
TextSpan("Build ").Style({.font_weight = FontWeight::SemiBold}),
TextSpan("passed. "),
TextSpan("Read the report").Link(Uri("https://example.com/report")),
};
return Column {
Text(summary).On<TextEvents::LinkActivated>([target](const TextLinkActivation& link) {
target = link.target.ToString();
}),
Text(target),
};
}

The example displays the requested URI; it does not open arbitrary external links. The application owns scheme validation and routing. Spans inherit unspecified style fields from the Text base style. Use AttributedText::FromRanges for parser-produced ranges and WithStyles when only syntax highlighting changes.

.Align(TextAlign), .VerticalAlign(TextVerticalAlign), and .Shaping(TextShapingOptions) configure paragraph placement/direction. Vertical alignment changes placement in extra height, not the intrinsic text height. Text range offsets are UTF-16 code units, not UTF-8 bytes.

Use .SelectionBlock(id) with a SelectionArea source when selection must reach unrealized content.

Need API
Display a resource without arguments Text(strings::title).
Display formatted literal or resource text Text::Format(pattern_or_resource, arguments...).
Obtain localized text for application logic or another API UseString(resource, arguments...) during composition.
Keep formatted resource text in an API accepting StringVariant StringVariant::Format(resource, arguments...).

The role-first Format overload takes TextRole before the literal/resource. Do not resolve a resource to an Asset or intermediate string merely to construct Text. A State<T> constructor reads its current printable value; it does not turn Text into an editor or emit changed events.

Style(TextStyle) replaces the base typography, while Shaping(TextShapingOptions) selects direction/locale and Align(TextAlign) positions paragraph content. VerticalAlign(TextVerticalAlign) needs extra assigned height to have an effect. Inline TextSpan styles inherit unspecified fields, so use run overrides for emphasis rather than rebuilding the entire paragraph style.

TextEvents::LinkActivated reports a TextLinkActivation; it does not implicitly open an external browser. Treat link targets as untrusted input when routing or launching another application. Parser-generated style/link ranges must use UTF-16 offsets into the same text snapshot, not bytes from a UTF-8 source buffer. Changing styles alone should preserve the underlying text and link identities where appropriate.