Formatting service
The formatting service changes the appearance and structure of text, including fonts, emphasis, alignment and lists. Commands act on the current selection or, where supported, set the style for text typed at the caret. Use Formatting to connect those commands to your own controls.
Use the State Query Service to inspect formatting and the Selection Service to choose a range. Formatting commands participate in the editor undo workflow; call MyEditor.Editor.Undo() to undo an applicable change.
The three FormatOptions values shared across the toggle methods are SetOn (always apply), SetOff (always remove), and Toggle (flip the current state). Toggle is the default for every overload that accepts the parameter, matching what end users expect from toolbar buttons such as Bold or Italic.
Namespace: SpiceLogic.HtmlEditor.WPF.Models.Services
Assembly: SpiceLogic.HtmlEditor.WPF
Declaration: public interface IFormattingService : IFormattingServiceBase
ChangeFont(string fontFamilyName, string fontSize, Color foreColor)
Applies a font family, size and foreground color to the current selection in one call. This is the bulk equivalent of issuing ChangeFontName, ChangeFontSize and ChangeForeColor back-to-back, but it produces a single undo step rather than three.
Syntax
void ChangeFont(string fontFamilyName, string fontSize, System.Windows.Media.Color foreColor);Parameters
| Name | Type | Description |
|---|---|---|
| fontFamilyName | String | CSS font-family name, for example "Calibri" or "Times New Roman". |
| fontSize | String | CSS-compatible size expression. Both legacy "1".."7" sizes and modern values such as "14px", "1.2em", "medium" are accepted. |
| foreColor | System.Windows.Media.Color | Foreground (text) color (WPF media color). |
Returns
None.
Remarks
The WPF surface intentionally takes System.Windows.Media.Color here rather than System.Drawing.Color so you can pass values straight from XAML brushes and color pickers. Requires EditorMode == WysiwygDesign; a no-op in HtmlEdit or Preview mode.
Example
using System.Windows.Media;
MyEditor.Formatting.ChangeFont("Calibri", "14px", Color.FromArgb(0xFF, 0x00, 0x33, 0x66));Imports System.Windows.Media
MyEditor.Formatting.ChangeFont("Calibri", "14px", Color.FromArgb(&HFF, &H00, &H33, &H66))ChangeFontName(string fontFamilyName)
Changes only the font family of the current selection. Equivalent to executing the FontName MSHTML command.
Syntax
void ChangeFontName(string fontFamilyName);Parameters
| Name | Type | Description |
|---|---|---|
| fontFamilyName | String | Any CSS-recognised font-family name. |
Returns
None.
Remarks
Requires EditorMode == WysiwygDesign; a no-op in HtmlEdit or Preview mode.
Example
private void OnFontComboSelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (FontCombo.SelectedItem is string name)
MyEditor.Formatting.ChangeFontName(name);
}Private Sub OnFontComboSelectionChanged(sender As Object, e As SelectionChangedEventArgs)
Dim name = TryCast(FontCombo.SelectedItem, String)
If name IsNot Nothing Then
MyEditor.Formatting.ChangeFontName(name)
End If
End SubChangeFontSize(string fontSize)
Changes only the font size of the current selection. The value is passed through to the underlying CSS font-size property, so any valid CSS size expression works (px, em, %, rem, named sizes).
Syntax
void ChangeFontSize(string fontSize);Parameters
| Name | Type | Description |
|---|---|---|
| fontSize | String | CSS-compatible size, for example "12px", "1.25em", "large". |
Returns
None.
Remarks
Requires EditorMode == WysiwygDesign; a no-op in HtmlEdit or Preview mode.
Example
MyEditor.Formatting.ChangeFontSize("16px");ChangeForeColor(Color foreColor)
Changes the foreground (text) color of the current selection. The WPF overload accepts a System.Windows.Media.Color so you can wire it directly to a XAML ColorPicker.
Syntax
void ChangeForeColor(System.Windows.Media.Color foreColor);Parameters
| Name | Type | Description |
|---|---|---|
| foreColor | System.Windows.Media.Color | New foreground color. |
Returns
None.
Remarks
Requires EditorMode == WysiwygDesign; a no-op in HtmlEdit or Preview mode.
Example
using System.Windows.Media;
MyEditor.Formatting.ChangeForeColor(Colors.Crimson);Imports System.Windows.Media
MyEditor.Formatting.ChangeForeColor(Colors.Crimson)ChangeHighlightColor(Color highlightColor)
Changes the background highlight color of the current selection. Maps to the underlying BackColor MSHTML command, producing a visual marker-pen effect.
Syntax
void ChangeHighlightColor(System.Windows.Media.Color highlightColor);Parameters
| Name | Type | Description |
|---|---|---|
| highlightColor | System.Windows.Media.Color | New background highlight color. |
Returns
None.
Remarks
Requires EditorMode == WysiwygDesign; a no-op in HtmlEdit or Preview mode.
Example
using System.Windows.Media;
MyEditor.Formatting.ChangeHighlightColor(Colors.Yellow);Imports System.Windows.Media
MyEditor.Formatting.ChangeHighlightColor(Colors.Yellow)Bold(FormatOptions option = FormatOptions.Toggle)
Applies, removes or toggles bold formatting on the current selection. With the default Toggle value the call behaves exactly like a toolbar Bold button or Ctrl+B.
Syntax
void Bold(FormatOptions option = FormatOptions.Toggle);Parameters
| Name | Type | Description |
|---|---|---|
| option | FormatOptions | SetOn, SetOff, or Toggle (default). |
Returns
None.
Remarks
In HtmlEdit mode this instead wraps the raw-HTML textbox's current selection in a <b> tag and ignores option - it always wraps, so calling it again nests the tag rather than toggling it off.
Example
using SpiceLogic.HtmlEditor.Abstractions;
MyEditor.Formatting.Bold(); // toggle
MyEditor.Formatting.Bold(FormatOptions.SetOn); // force on
Imports SpiceLogic.HtmlEditor.Abstractions
MyEditor.Formatting.Bold() ' toggle
MyEditor.Formatting.Bold(FormatOptions.SetOn) ' force onItalic(FormatOptions option = FormatOptions.Toggle)
Applies, removes or toggles italic formatting on the current selection.
Syntax
void Italic(FormatOptions option = FormatOptions.Toggle);Parameters
| Name | Type | Description |
|---|---|---|
| option | FormatOptions | SetOn, SetOff, or Toggle (default). |
Returns
None.
Remarks
In HtmlEdit mode this instead wraps the raw-HTML textbox's current selection in a <i> tag and ignores option - it always wraps, so calling it again nests the tag rather than toggling it off.
Example
MyEditor.Formatting.Italic();Underline(FormatOptions option = FormatOptions.Toggle)
Applies, removes or toggles underline formatting on the current selection.
Syntax
void Underline(FormatOptions option = FormatOptions.Toggle);Parameters
| Name | Type | Description |
|---|---|---|
| option | FormatOptions | SetOn, SetOff, or Toggle (default). |
Returns
None.
Remarks
In HtmlEdit mode this instead wraps the raw-HTML textbox's current selection in a <u> tag and ignores option - it always wraps, so calling it again nests the tag rather than toggling it off.
Example
MyEditor.Formatting.Underline();Superscript(FormatOptions option = FormatOptions.Toggle)
Applies, removes or toggles superscript formatting on the current selection. Mutually exclusive with subscript at the same caret - enabling superscript automatically clears subscript.
Syntax
void Superscript(FormatOptions option = FormatOptions.Toggle);Parameters
| Name | Type | Description |
|---|---|---|
| option | FormatOptions | SetOn, SetOff, or Toggle (default). |
Returns
None.
Remarks
In HtmlEdit mode this instead wraps the raw-HTML textbox's current selection in a <sup> tag and ignores option - it always wraps, so calling it again nests the tag rather than toggling it off.
Example
MyEditor.Formatting.Superscript(FormatOptions.SetOn);Subscript(FormatOptions option = FormatOptions.Toggle)
Applies, removes or toggles subscript formatting on the current selection. Mutually exclusive with superscript.
Syntax
void Subscript(FormatOptions option = FormatOptions.Toggle);Parameters
| Name | Type | Description |
|---|---|---|
| option | FormatOptions | SetOn, SetOff, or Toggle (default). |
Returns
None.
Remarks
In HtmlEdit mode this instead wraps the raw-HTML textbox's current selection in a <sub> tag and ignores option - it always wraps, so calling it again nests the tag rather than toggling it off.
Example
MyEditor.Formatting.Subscript(FormatOptions.SetOn);StrikeThrough(FormatOptions option = FormatOptions.Toggle)
Applies, removes or toggles strike-through formatting on the current selection.
Syntax
void StrikeThrough(FormatOptions option = FormatOptions.Toggle);Parameters
| Name | Type | Description |
|---|---|---|
| option | FormatOptions | SetOn, SetOff, or Toggle (default). |
Returns
None.
Remarks
In HtmlEdit mode this instead wraps the raw-HTML textbox's current selection in a <strike> tag and ignores option - it always wraps, so calling it again nests the tag rather than toggling it off.
Example
MyEditor.Formatting.StrikeThrough();Outdent()
Decreases the left indent of the paragraph containing the current selection. If the paragraph is already at the document margin the call is a no-op.
Syntax
void Outdent();Parameters
None.
Returns
None.
Remarks
A no-op in HtmlEdit mode.
Example
MyEditor.Formatting.Outdent();Indent()
Increases the left indent of the paragraph containing the current selection. Inside a list, Indent promotes the bullet or numbered item to a deeper nesting level rather than shifting the whole item right.
Syntax
void Indent();Parameters
None.
Returns
None.
Remarks
A no-op in HtmlEdit mode.
Example
MyEditor.Formatting.Indent(); // nest the current bullet one level
MyEditor.Formatting.Outdent(); // promote it backAlignLeft()
Left-aligns the paragraph containing the current selection.
Syntax
void AlignLeft();Parameters
None.
Returns
None.
Remarks
A no-op in HtmlEdit mode.
Example
MyEditor.Formatting.AlignLeft();AlignRight()
Right-aligns the paragraph containing the current selection.
Syntax
void AlignRight();Parameters
None.
Returns
None.
Remarks
A no-op in HtmlEdit mode.
Example
MyEditor.Formatting.AlignRight();AlignCenter()
Center-aligns the paragraph containing the current selection.
Syntax
void AlignCenter();Parameters
None.
Returns
None.
Remarks
A no-op in HtmlEdit mode.
Example
MyEditor.Formatting.AlignCenter();RemoveAlignment()
Removes any explicit horizontal-alignment style from the paragraph containing the current selection, returning it to the document default.
Syntax
void RemoveAlignment();Parameters
None.
Returns
None.
Remarks
"Justify" (full justification) is intentionally not exposed - it was retired from the WPF control because MSHTML's rendering of justified text proved inconsistent across document widths. A no-op in HtmlEdit mode.
Example
MyEditor.Formatting.RemoveAlignment();ApplyList(ListTypes listType, FormatOptions option = FormatOptions.Toggle)
Converts the current selection to a bullet or numbered list, or toggles list formatting off when the selection is already inside a list of that type.
Syntax
void ApplyList(ListTypes listType, FormatOptions option = FormatOptions.Toggle);Parameters
| Name | Type | Description |
|---|---|---|
| listType | ListTypes | BulletList renders <ul>, NumberedList renders <ol>. |
| option | FormatOptions | SetOn, SetOff, or Toggle (default). |
Returns
None.
Remarks
Requires EditorMode == WysiwygDesign; a no-op in HtmlEdit or Preview mode.
Example
using SpiceLogic.HtmlEditor.Abstractions;
MyEditor.Formatting.ApplyList(ListTypes.NumberedList);Imports SpiceLogic.HtmlEditor.Abstractions
MyEditor.Formatting.ApplyList(ListTypes.NumberedList)RemoveFormatFromSelection()
Strips inline formatting (font, color, bold/italic/underline, etc.) from the current selection while preserving block structure such as paragraphs and lists. Useful when pasting from sources that carry inline color or font choices the host application does not want.
Syntax
void RemoveFormatFromSelection();Parameters
None.
Returns
None.
Remarks
A no-op in HtmlEdit mode.
Example
MyEditor.Formatting.RemoveFormatFromSelection();RemoveFormatAll()
Strips inline formatting from the entire document body. This is a heavier sibling of RemoveFormatFromSelection that does not require a prior selection.
Syntax
void RemoveFormatAll();Parameters
None.
Returns
None.
Remarks
Equivalent to MyEditor.Selection.SelectAll() followed by MyEditor.Formatting.RemoveFormatFromSelection(), but it leaves the previous selection state intact afterwards. Requires EditorMode == WysiwygDesign; a no-op in HtmlEdit or Preview mode.
Example
if (MessageBox.Show("Strip all inline formatting?", "Confirm", MessageBoxButton.YesNo) == MessageBoxResult.Yes)
{
MyEditor.Formatting.RemoveFormatAll();
}If MessageBox.Show("Strip all inline formatting?", "Confirm", MessageBoxButton.YesNo) = MessageBoxResult.Yes Then
MyEditor.Formatting.RemoveFormatAll()
End IfApplyTitleHeader(int headerNumber)
Promotes the paragraph containing the current selection to an HTML heading (<h1> through <h6>).
Syntax
void ApplyTitleHeader(int headerNumber);Parameters
| Name | Type | Description |
|---|---|---|
| headerNumber | Int32 | Heading level. Valid values are 1 through 6. |
Returns
None.
Remarks
Pass 0 to remove the heading instead of applying one - equivalent to calling RemoveHeaderTitle. Values outside 0-6 are silently ignored.
Example
// Wrap the current paragraph in <h2>.
MyEditor.Formatting.ApplyTitleHeader(2);RemoveHeaderTitle()
Removes any heading wrapper from the paragraph containing the current selection, restoring it to a plain <p> paragraph.
Syntax
void RemoveHeaderTitle();Parameters
None.
Returns
None.
Remarks
When the selection covers only part of a heading, the heading is split so the selected part is demoted to a paragraph and the rest keeps its heading level.
Example
MyEditor.Formatting.RemoveHeaderTitle();Dispose()
Inherited from IDisposable. Called by the editor control during teardown - host applications normally do not invoke this directly, because disposing the editor disposes its services.
Syntax
void Dispose();Parameters
None.
Returns
None.
Example
// Normally unnecessary - the editor disposes its services.
// Shown here only for completeness.
((IDisposable)MyEditor.Formatting).Dispose();Ask your AI to do this
Connect the SpiceLogic MCP server, then give your coding assistant this prompt.
I use the SpiceLogic WPF HTML Editor. Help me apply text and paragraph formatting through the Formatting service. Confirm the exact API names, types and defaults with the SpiceLogic MCP documentation, then show the changes for my project.