MintedTextEditor supports two levels of formatting: character formatting (applied to inline text runs) and paragraph formatting (applied to entire block-level elements).
All formatting APIs take the document and a TextRange. In the MAUI control, use editor.Document and editor.Selection.Range:
using MintedTextEditor.Core.Document;
using MintedTextEditor.Core.Formatting;
using MintedTextEditor.Core.Rendering;
var doc = editor.Document;
var range = editor.Selection.Range;Character formatting is represented by TextStyle, an immutable class. All With… methods return a new TextStyle rather than mutating in place.
The recommended way to apply character formatting is through FormattingEngine (editor.Formatting), which handles splitting and merging runs automatically. A toggle turns the format on unless the whole range already has it.
var fmt = editor.Formatting;
fmt.ToggleBold(doc, range);
fmt.ToggleItalic(doc, range);
fmt.ToggleUnderline(doc, range);
fmt.ToggleStrikethrough(doc, range);
fmt.ToggleSubscript(doc, range);
fmt.ToggleSuperscript(doc, range);
fmt.ClearFormatting(doc, range);With an empty range (a collapsed caret), toggles set a pending style that is applied to the next text the user types. The pending style starts from the formatting at the caret, so toggling Bold inside bold text turns bold off for the next text and keeps the other formatting. The pending style applies only at the caret where it was picked (PendingStylePosition): moving the caret or selecting text discards it, while Enter carries it into the new paragraph. Hosts that move the selection themselves can call DiscardPendingStyleIfMoved(doc, selection) on FormattingEngine and FontFormattingEngine; the input controller, toolbar and MintedEditorView already do.
Without a pending style, typed text inherits the formatting of the character before the caret (or the character after it at the start of a paragraph; an empty paragraph uses TextStyle.Default). Pressing Enter carries that formatting into the new paragraph, so typing after Enter at the end of bold text stays bold. Pasted plain text gets the same style as typed text. Typing or pasting at the start or end of a hyperlink with nothing selected adds plain text next to it rather than extending the link; text that replaces a selection inside a link (typing over it, or find and replace) stays in the link. DocumentEditor.GetInsertionStyle(doc, position) returns that inherited style, and FormattingEngine.GetEffectiveCaretStyle(doc, position, editor.Formatting.PendingStyle, editor.FontFormatting.PendingStyle) returns the style the next typed text will get. The toolbar's Bold, Italic, Underline, Strikethrough, Subscript and Superscript buttons show that style when nothing is selected.
Font family, size and colors go through FontFormattingEngine (editor.FontFormatting):
var font = editor.FontFormatting;
font.ApplyFontFamily(doc, range, "Georgia");
font.ApplyFontSize(doc, range, 18f);
font.ApplyTextColor(doc, range, new EditorColor(255, 0, 0)); // red
font.ApplyHighlightColor(doc, range, new EditorColor(255, 255, 0, 64)); // semi-transparent yellow
font.RemoveHighlightColor(doc, range);EditorColor takes (r, g, b, a = 255); see Theming.
The engines above change the document directly and are not recorded for undo. The toolbar, the keyboard shortcuts and the character-formatting commands (ToggleBoldCommand … ClearFormattingCommand, ApplyFontFamilyCommand, ApplyFontSizeCommand, ApplyTextColorCommand, ApplyHighlightColorCommand) are: each records one undo step. From code, run one of those commands with an EditorContext (see Commands & Events), or push an ApplyStyleAction; UndoManager.Push executes it and records it:
using MintedTextEditor.Core.Editing;
var action = new ApplyStyleAction(doc, range, s => s
.WithBold(true)
.WithFontSize(14f));
editor.UndoManager.Push(action); // executes and records undo
// Or wrap an engine call, e.g. a toggle that decides on/off from the current text
editor.UndoManager.Push(new ApplyStyleAction(doc, range,
r => editor.Formatting.ToggleBold(doc, r), "Bold"));Undo restores the runs exactly as they were, styles included. Push an ApplyStyleAction only for a non-empty range; with a collapsed caret, call the engine directly to set the pending style.
TextStyle style = FontFormattingEngine.GetCurrentTextStyle(doc, editor.Selection.Active);
bool isBold = style.IsBold;
string family = style.FontFamily;
// Common style over a range, or null when the range is empty
TextStyle? common = FontFormattingEngine.GetTextStyleForRange(doc, range);Paragraph formatting is stored in ParagraphStyle on each Paragraph block. ParagraphFormattingEngine is a static class; each method applies to every paragraph the range touches.
The toolbar applies paragraph formatting through the paragraph commands (namespace MintedTextEditor.Core.Commands): AlignLeftCommand, AlignCenterCommand, AlignRightCommand, AlignJustifyCommand, ToggleBulletListCommand, ToggleNumberListCommand, IncreaseIndentCommand, DecreaseIndentCommand, SetHeadingLevelCommand and SetParagraphFormatCommand. These are recorded for undo: each run is one undo step covering every paragraph in the selection. The ParagraphFormattingEngine methods below change the document directly and are not recorded for undo.
using MintedTextEditor.Core.Commands;
var ctx = new EditorContext(editor.Document, editor.Selection, editor.UndoManager,
editor.Formatting, editor.FontFormatting);
new AlignCenterCommand().Execute(ctx); // undoable
new SetHeadingLevelCommand(2).Execute(ctx); // undoableTo make an engine call (or any other paragraph style change) undoable, wrap it in an ApplyParagraphStyleAction. It snapshots the style of each paragraph in the range before and after, so undo restores the old styles and redo reapplies the new ones:
using MintedTextEditor.Core.Editing;
editor.UndoManager.Push(new ApplyParagraphStyleAction(doc, range,
(d, r) => ParagraphFormattingEngine.SetLineSpacing(d, r, 1.5f)));ParagraphFormattingEngine.SetAlignment(doc, range, TextAlignment.Center);
ParagraphFormattingEngine.SetAlignment(doc, range, TextAlignment.Justify);Available values: Left · Center · Right · Justify
ParagraphFormattingEngine.SetHeadingLevel(doc, range, 1); // H1
ParagraphFormattingEngine.SetHeadingLevel(doc, range, 2); // H2
ParagraphFormattingEngine.SetHeadingLevel(doc, range, 0); // body textParagraphFormattingEngine.ToggleBulletList(doc, range);
ParagraphFormattingEngine.ToggleNumberList(doc, range);
ParagraphFormattingEngine.IncreaseIndent(doc, range);
ParagraphFormattingEngine.DecreaseIndent(doc, range);ParagraphFormattingEngine.SetLineSpacing(doc, range, 1.5f);
// Other ParagraphStyle properties have no dedicated method; set them through DocumentEditor
DocumentEditor.ApplyParagraphStyle(doc, range, s =>
{
s.IndentLevel = 2;
s.SpaceBefore = 8f;
s.SpaceAfter = 8f;
});ParagraphFormattingEngine.SetParagraphFormat(doc, range, "Quote"); // also "Normal", "Heading1"–"Heading6"DocumentEditor.ApplyParagraphStyle(doc, range, s => s.Direction = TextDirection.RightToLeft);Typing, deleting, pasting, the insert commands (hyperlink, image, table, rows and columns), removing a hyperlink, the paragraph commands (alignment, lists, indent, headings, paragraph format), and character formatting from the toolbar (buttons, font family and size dropdowns, text and highlight colour pickers), keyboard shortcuts and commands are recorded by the UndoManager, one step per action. Toggling a format with a collapsed caret only sets the pending style and is not recorded. The formatting engines (FormattingEngine, FontFormattingEngine, ParagraphFormattingEngine) are not recorded; push an ApplyStyleAction or ApplyParagraphStyleAction (above) when formatting from code must be undoable.
editor.Undo(); // or editor.UndoManager.Undo()
editor.Redo();editor.Undo()/Redo() also move the caret to the restored position and raise SelectionChanged when it moves.
var doc = editor.Document;
var para = (Paragraph)doc.Blocks[0];
// Check inline runs
foreach (var inline in para.Inlines)
{
if (inline is TextRun run)
Console.WriteLine($"{run.Text}: bold={run.Style.IsBold}");
}
// Check paragraph style
Console.WriteLine(para.Style.Alignment);
Console.WriteLine(para.Style.HeadingLevel);