Table of Contents

Programmatic formatting

Every formatting action the toolbar performs is also a public method on the editor. This is the chapter to read when you want to apply formatting from code — to offer keyboard shortcuts, to format content during an import, or to build your own toolbar instead of (or alongside) the built-in one. The methods act on the current selection or, when nothing is selected, at the caret position, exactly as the equivalent toolbar buttons do.

A TTMSFNCWXHTMLMemo showing the result of formatting commands — a heading, bold, italic and underlined runs, a highlighted span, a hyperlink and a bulleted list.

Character formatting

TextBold, TextItalic, TextUnderline, and TextStrikeThrough each toggle a character style. RemoveFormat strips all character formatting from the selection, and SetFontName / SetFontSize set the font family and size. Font size is interpreted in the active font-size unit.

Paragraph and layout

Alignment is handled by JustifyLeft, JustifyRight, JustifyCenter, and JustifyFull. Indent and Outdent change the indentation level, SetLineHeight sets line spacing as a multiple of the font size, and FormatToParagraphs wraps the selection in paragraph elements.

Inserting structure

InsertList(AOrdered: Boolean) inserts a numbered list when AOrdered is True and a bulleted list when False. InsertParagraph adds a paragraph break, InsertText inserts a plain-text run, and PasteHTML inserts a formatted HTML fragment at the caret.

Undo, redo, and code view

Undo and Redo walk the editor's edit history. ToggleCodeview switches between the visual editing surface and the raw HTML source, letting power users edit markup directly.

procedure TForm1.ApplyFormatting;
begin
  { Character formatting toggles the style on the current selection or caret. }
  TMSFNCWXHTMLMemo1.TextBold;
  TMSFNCWXHTMLMemo1.TextItalic;
  TMSFNCWXHTMLMemo1.TextUnderline;
  TMSFNCWXHTMLMemo1.TextStrikeThrough;

  { Apply a font family and size (size is expressed in the active FontSizeUnit). }
  TMSFNCWXHTMLMemo1.SetFontName('Georgia');
  TMSFNCWXHTMLMemo1.SetFontSize(14);

  { Paragraph layout. }
  TMSFNCWXHTMLMemo1.JustifyCenter;
  TMSFNCWXHTMLMemo1.SetLineHeight(2);
  TMSFNCWXHTMLMemo1.Indent;

  { Insert structure: True = ordered (numbered) list, False = bulleted list. }
  TMSFNCWXHTMLMemo1.InsertList(True);
  TMSFNCWXHTMLMemo1.InsertText('A plain-text run');
  TMSFNCWXHTMLMemo1.PasteHTML('<mark>highlighted fragment</mark>');

  { Clear all character formatting from the selection. }
  TMSFNCWXHTMLMemo1.RemoveFormat;
end;

procedure TForm1.btnUndoClick(Sender: TObject);
begin
  TMSFNCWXHTMLMemo1.Undo;
end;

procedure TForm1.btnRedoClick(Sender: TObject);
begin
  TMSFNCWXHTMLMemo1.Redo;
end;

Combining programmatic formatting with a hidden toolbar

The most common reason to format from code is to replace the built-in toolbar with your own. Hide the toolbar with Toolbar.Visible := False and call these methods from your buttons; use OnStyleTextUpdate to reflect the caret's current style back onto your UI. The Building a custom toolbar chapter walks through the full pattern end to end.

Pitfalls

  • Calling formatting methods before OnInit. The methods are forwarded to the embedded editor; calling them before it has initialized has no effect. Drive code-only formatting from a button click (after the user has interacted) or from OnInit.
  • Font size in the wrong unit. SetFontSize uses the value in the current FontSizeUnit. A size of 14 means 14pt under mfuPt and 14px under mfuPx.

See also