waterui-text
Text and typography components for WaterUI with rich styling, fonts, markdown, and syntax highlighting.
Overview
waterui-text provides comprehensive text rendering and formatting capabilities for the WaterUI framework. It handles everything from simple text display to complex styled text with multiple font properties, inline-focused markdown styling, and syntax highlighting for code snippets across 40+ programming languages.
The crate is designed around reactive primitives, automatically updating text when underlying data changes. All text rendering delegates to native platform widgets (UIKit/AppKit on Apple, Android View on Android), ensuring platform-native appearance and accessibility.
Core features include semantic font styles (body, title, headline), granular styling control (bold, italic, underline, colors), inline markdown parsing for styled text runs, and production-ready syntax highlighting via syntect.
Installation
Add to your Cargo.toml:
[]
= "0.1.0"
Or use the main WaterUI crate which re-exports text components:
[]
= "0.3"
Quick Start
use text;
// Simple text
let greeting = text;
// Styled text with method chaining
let title = text.bold.title.foreground;
// Reactive text that updates automatically
let count = binding;
let counter_text = text!;
Core Concepts
Text Component
The Text struct is the primary component for displaying read-only text. It automatically sizes itself to fit content and wraps when constrained by width. Text never stretches to fill extra space—it behaves like a label.
StyledStr
StyledStr represents rich text with multiple styling attributes. It stores text as chunks, each with independent font, color, and decoration properties. This enables inline formatting like bold and italic within a single text component.
Font System
Fonts are resolved through the Environment, allowing dynamic theming. The crate provides semantic font styles:
Body(16pt, Normal)Title(24pt, SemiBold)Headline(32pt, Bold)Subheadline(20pt, SemiBold)Caption(12pt, Normal)Footnote(10pt, Light)
Markdown Support
StyledStr::from_markdown() focuses on inline semantics (headings/emphasis/strong/strikethrough/inline code) and lightweight block separators. For full block-level markdown rendering, use RichText via include_markdown!() in the main waterui crate.
Examples
Basic Text with Styling
use ;
use Color;
// Simple text
text
// Bold text with custom size
text
.bold
.size
// Custom font and color
text
.font
.weight
.foreground
// Text with background
text
.background_color
.foreground
Reactive Text with Formatting
use *;
use binding;
let custom_name = binding;
let custom_count = binding;
let custom_slider = binding;
// Reactive text updates automatically when bindings change
vstack
Markdown Rendering
use *;
Localized Text
use Text;
// Static keys resolve through the environment's translation catalog and
// update when the native platform locale changes.
let title = localized;
// Runtime strings remain verbatim.
let server_message = verbatim;
Styled Text Construction
use ;
use ;
use Color;
// Build styled text from chunks
let mut styled = empty;
styled.push;
styled.push;
styled.push;
// Parse markdown
let markdown = from_markdown;
// Apply styling to all chunks
let blue_text = styled.foreground;
Syntax Highlighting
use ;
use Str;
use ColorScheme;
// Create highlighter with the palette for the surface it is read against
let mut highlighter = new;
// Highlight code
let code = from;
let highlighted = highlight_text;
API Overview
Main Types
Text- Primary text display component with styling methodstext(content)- Convenience function to create text componentstext!(format, args...)- Macro for reactive formatted textStyledStr- Rich text with multiple style chunksStyle- Text attributes (font, color, italic, underline, strikethrough)Font- Font configuration with semantic stylesFontWeight- Font weight enumeration (Thin to Black)
Text Methods
.bold()- Apply bold weight.italic(bool)- Toggle italic style.underline(bool)- Toggle underline decoration.size(f64)- Set font size in points.weight(FontWeight)- Set font weight.font(Font)- Set complete font configuration.foreground(Color)- Set text color.background_color(Color)- Set background color.body(),.title(),.headline(), etc. - Apply semantic font styles
StyledStr Methods
StyledStr::plain(text)- Create plain styled textStyledStr::from_markdown(md)- Parse markdown into styled text.push(text, style)- Add styled chunk.bold(),.italic(bool),.underline(bool)- Apply styling to all chunks.foreground(color),.background_color(color)- Color all chunks.to_plain()- Extract plain text without styling
Syntax Highlighting
Language- Enum of supported languages (Rust, Swift, Python, Javascript, etc.)DefaultHighlighter- Syntect-based highlighter with 40+ languageshighlight_text(lang, text, highlighter)- Highlight code into styled text chunks
Localization
Text::localized(key)- Resolve a static translation key reactivelyText::localized_with(resolver)- Build locale-aware text configurationFormatter<T>andText::format- Format reactive values without rebuilding a view
Features
highlight(default) —DefaultHighlighter, the bundled syntect/two-face syntax highlighter, and theCodefenced-code widget built on it.markdown—StyledStr::from_markdownand thepulldown-cmarkgrammar behind it.font-collection—FontCollection, the oneparleyfont collection a host installs into theEnvironmentand every component that shapes or typesets text itself reads back out. Turned on by those components, not by applications: text drawn by the platform's own engine never reaches for it, and a build that does not need it carries neither the type nor the font stack behind it.system-fonts— discovering the platform's fonts, which is how a host that owns no font stack of its own obtains the collection it installs throughinstall_system_font_collection.
Dependencies
Key dependencies that shape the API:
- waterui-core - Provides
Viewtrait,Environment, and reactive primitives - nami - Fine-grained reactivity system (
Binding,Computed,Signal) - waterui-graphics - Color types for text and background styling
- pulldown-cmark - Markdown parsing engine
- syntect - Syntax highlighting for code blocks
- two-face - Extended syntax definitions including Swift