TextFitting Class

Fits texts in the space a layout gives them: truncation with an ellipsis, and wrapping on several lines.

public static class TextFitting

Methods

NameDescription
Truncate(ITextMeasurer, string, SceneFont, double)Shortens a text by whole grapheme clusters and appends an ellipsis, so it fits a width.
Wrap(ITextMeasurer, string, SceneFont, double)Splits a text into lines that fit a width. The text is broken at the line break opportunities of the Unicode line breaking algorithm (UAX #14), and at its line feeds; a word wider than the width is broken between grapheme clusters.

Truncate(ITextMeasurer, string, SceneFont, double)

TextFitting.cs

Shortens a text by whole grapheme clusters and appends an ellipsis, so it fits a width.

public static string? Truncate(ITextMeasurer measurer, string text, SceneFont font, double maxWidth)

Parameters

measurer ITextMeasurer
The measurer of the font that draws the text.
text string
The text, on one line.
font SceneFont
The font.
maxWidth double
The available width.

Returns

string? The text when it fits, the shortened text, or null when not even the first character fits with the ellipsis.

Wrap(ITextMeasurer, string, SceneFont, double)

TextFitting.cs

Splits a text into lines that fit a width. The text is broken at the line break opportunities of the Unicode line breaking algorithm (UAX #14), and at its line feeds; a word wider than the width is broken between grapheme clusters.

public static IReadOnlyList<string> Wrap(ITextMeasurer measurer, string text, SceneFont font, double maxWidth)

Parameters

measurer ITextMeasurer
The measurer of the font that draws the text.
text string
The text.
font SceneFont
The font.
maxWidth double
The available width. A value that is not positive and finite keeps every line of the text whole.

Returns

IReadOnlyList<string> The lines, without their trailing spaces and line terminators. An empty text returns one empty line.

Constants

NameDescription
EllipsisThe ellipsis TextFitting.Truncate appends, … (U+2026).

Ellipsis

The ellipsis TextFitting.Truncate appends, … (U+2026).

public const string Ellipsis = "…"