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, markdown rendering with full formatting support, 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), markdown parsing with inline and block elements, 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.2"
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
The StyledStr::from_markdown() function parses markdown into styled text, supporting headings, emphasis, strong, code blocks, lists, tables, links, and horizontal rules.
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
From /Users/lexoliu/Coding/waterui/examples/form/src/lib.rs:
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
From /Users/lexoliu/Coding/waterui/examples/markdown/src/lib.rs:
use *;
Custom Formatting with Locales
use ;
use Date;
// Create a date formatter respecting locale
let formatter = DateFormatter ;
let date = binding;
// Format date with custom formatter
let formatted = format;
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;
// Create highlighter
let highlighter = new;
// Highlight code asynchronously
let code = from;
let highlighted = highlight_text.await;
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)- Async highlighting function
Localization
Formatter<T>- Trait for locale-aware formattingDateFormatter- Date formatting with locale supportLocale- Locale identifier wrapper
Features
This crate has no optional features. All functionality is included by default.
Dependencies
Key dependencies that shape the API:
- waterui-core - Provides
Viewtrait,Environment, and reactive primitives - nami - Fine-grained reactivity system (
Binding,Computed,Signal) - waterui-color - 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