Formatting Service
The Formatting Service exposes every character-level, paragraph-level and structural formatting command the WpfHtmlEditor control supports. Each member acts on the current selection; when no text is selected most commands toggle the formatting state at the caret so that subsequent typing inherits the new style. The service is reached through the editor.Formatting property and is typed as SpiceLogic.HtmlEditor.WPF.Models.Services.IFormattingService, which derives from the shared IFormattingServiceBase / IFormattingService.
All methods are fire-and-forget — they raise no exceptions for an empty selection and return void. Use the companion State Query Service to discover the current formatting state before or after invoking these methods, and the Selection Service to size and trim a selection programmatically. Every call participates in the editor undo stack, so it is reversible via MyEditor.Undo().
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.
Example
using System.Windows.Media;
MyEditor.Formatting.ChangeFont("Calibri", "14px",
Color.FromArgb(0xFF, 0x00, 0x33, 0x66));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.
Example
private void OnFontComboSelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (FontCombo.SelectedItem is string name)
MyEditor.Formatting.ChangeFontName(name);
}ChangeFontSize(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.
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.
Example
using 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.
Example
using 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.
Example
using 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.
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.
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.
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.
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.
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.
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.
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.
Example
MyEditor.Formatting.AlignLeft();AlignRight()
Right-aligns the paragraph containing the current selection.
Syntax
void AlignRight();Parameters
None.
Returns
None.
Example
MyEditor.Formatting.AlignRight();AlignCenter()
Center-aligns the paragraph containing the current selection.
Syntax
void AlignCenter();Parameters
None.
Returns
None.
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.
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.
Example
using 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.
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.
Example
if (MessageBox.Show("Strip all inline formatting?", "Confirm",
MessageBoxButton.YesNo) == MessageBoxResult.Yes)
{
MyEditor.Formatting.RemoveFormatAll();
}ApplyTitleHeader(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.
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.
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();