Styled text editor
A rich-text editor lets people write content and format its structure and emphasis. It suits documents and messages that need headings, lists, or styled passages alongside the words themselves.
text_editor(doc)
edits a StyledText, the same document a label renders and
.markdown()
produces, live in each platform’s text editor: bold, italic, underline, strikethrough, relative
size, text and highlight color, plus paragraph alignment, indent, spacing and list markers.
Add to your app
Add day-piece-text-editor to your app:
cargo add 'day-piece-text-editor@0.*'
day build -t <target> enables this direct dependency’s matching toolkit feature
automatically. No dependency entry is needed in Day.toml.
| Platforms / toolkits | Availability |
|---|---|
| AppKit, UIKit, GTK, Qt, Android, Web | Native or browser rich-text editing. |
| WinUI, ArkUI | Editor implementation is present; selection and editing limitations below remain unverified. |
Authoring
use day::prelude::*;
use day_piece_text_editor::text_editor;
// Synthetic editor content, representing a document supplied by the user.
let doc = Signal::new(StyledText::markdown("**Hello**, _world_.", Font::Body));
let sel = Signal::new(0..0);
let typing = Signal::new(RunStyle::plain(Font::Body));
column((
// Add the formatting actions the app needs.
button(res::str::bold()).action(move || {
let range = sel.get_untracked();
let on = !doc
.with_untracked(|d| d.style_of(range.clone(), Font::Body))
.bold();
if range.is_empty() {
typing.update(|style| style.set_bold(!style.bold()));
} else {
doc.update(|d| d.apply(range.clone(), Font::Body, move |s| s.set_bold(on)));
}
}),
text_editor(doc)
.selection(sel) // two-way: caret moves write it, writing it moves the caret
.typing_style(typing) // style for newly typed characters
.placeholder(res::str::start_typing())
.min_lines(8)
.max_lines(14),
))
The UI snippets use generated res::str accessors from an app’s Fluent catalog; see the
example messages and catalog setup.
Document and selection
StyledText contains the text,
character runs, and paragraph runs. Character styles include font, color, background,
underline, strikethrough, and links. Paragraph styles include alignment, indentation, list
markers, and spacing. Labels can render the same document; see Styled text.
Selections are byte ranges into the Rust string, including on platforms that use other index
units internally. A collapsed range is a caret. Bind the range with
.selection(binding)
to observe or move it. Keep programmatic ranges on valid UTF-8 boundaries.
Use style_of
to inspect the selected style and
apply to change
character styles. apply_paragraph
changes the paragraphs touched by a range. For an empty selection, update the
.typing_style(binding)
binding to style the next characters the person types.
FontSpec::scale scales a semantic font while retaining its response to text-size settings.
Font::System(points) specifies an absolute size. The editor’s
.base(font)
defaults to Font::Body.
Editing and formatting
Native editing updates the bound document. Changes to its styles restyle the text; an app replacement of the document updates the editor. Formatting actions should write the document or typing-style binding. The editor supplies no formatting toolbar.
Pasting styled text keeps the characters and uses the surrounding style. Native formatting controls are disabled where supported because formatting is owned by the bound document. Use app commands for bold, italic, and other formatting, and check the platform limits below.
.editable(false)
makes the editor read-only. Use
.placeholder(text)
for a localized empty-state prompt and
.min_lines(n) /
.max_lines(n)
to control its height.
Import and export
StyledText provides
markdown,
html, and
rtf parsers, with
to_markdown,
to_html, and
to_rtf exporters.
These conversions are lossy. Markdown cannot retain arbitrary color, background, or font size; a heading scale outside its six levels exports as bold. The RTF reader handles Day’s output and common paragraph control words, rather than the full RTF format. Keep the original user document when an import/export round trip must preserve unsupported formatting.
Code and single-line editors
text_editor_text(binding)
edits a string binding when the app does not store a styled document.
.highlight(f)
derives text runs after edits and when signals read by the highlighter change. The crate’s
highlighter
supports JSON with comments, XML, HTML, GraphQL, Rust, and plain text.
Language::for_mime
selects a language from a content type. Supply a palette appropriate for the editor’s appearance.
.single_line()
disables wrapping, scrolls horizontally, and converts pasted newlines to spaces. Enter invokes
.on_submit(f)
without inserting a newline.
.code()
adds indentation and paired bracket/quote insertion. Tab inserts spaces to the next stop;
.indent(n)
sets its width, defaulting to two. Enter carries forward indentation and adds a level after
an opening bracket. Code mode disables spell-check and automatic prose substitutions where
the toolkit supports them. Use
.no_spellcheck()
when only spell-check should be disabled.
Toolkit implementations
| Toolkit | Editor | Formatting limits |
|---|---|---|
| AppKit | NSTextView | Uses TextKit character and paragraph styles. |
| UIKit | UITextView | Uses TextKit character and paragraph styles. |
| GTK | GtkTextView | Dotted underline becomes a single rule; wavy underline uses the error underline. |
| Qt | QTextEdit | Double underline becomes a single rule. |
| Android | EditText | Dotted and wavy underlines become a single rule; paragraph justification and paragraph spacing are unavailable. |
| WinUI | RichEditBox | See the native editing validation limits below. |
| ArkUI | RichEditor | One decoration per span; strikethrough takes precedence over underline. |
| Web | Browser contenteditable | Browser editing and selection behavior applies. |
Toolkits without a styled editor, including the mock toolkit, fall back to plain text and
report Support::Emulated. The fallback preserves text but does not display its formatting.