Class RichText
Text with styles: a string, the StyleSpans over it (sorted, never overlapping) and the Placeholders for its object replacement characters, in order. Immutable; build one with RichTextBuilder.
public sealed class RichText
- Inheritance
-
RichText
- Inherited Members
Examples
// Font families come from a collection; later families cover what the first lacks.
FontCollection fonts = new();
fonts.AddFile(System.IO.Path.Combine(folder, "NotoSans-Regular.ttf"));
fonts.AddFile(System.IO.Path.Combine(folder, "NotoSansArabic-Regular.ttf"));
fonts.TryGetFamily("Noto Sans", out FontFamily latin);
fonts.TryGetFamily("Noto Sans Arabic", out FontFamily arabic);
// Styles nest: the inner span keeps the outer one's size and adds a color and an underline.
RichText text = new RichTextBuilder()
.Append("Peace is ")
.Push(new TextStyle { Size = 20 })
.Append("\u0633\u0644\u0627\u0645 ")
.Push(new TextStyle { Color = new Rgba8(200, 30, 30, 255), Decoration = new TextDecoration() })
.Append("in Arabic")
.Pop()
.Pop()
.AddPlaceholder(new Placeholder(16, 16, PlaceholderAlignment.Middle))
.ToRichText();
ParagraphStyle style = new()
{
DefaultStyle = new TextStyle { FontFamilies = [latin, arabic], Size = 16, LineHeight = LineHeight.Multiple(1.4f) },
Align = TextAlign.Justify,
LineBreaking = new LineBreakOptions(WordBreak: WordBreak.Normal),
};
// Shaped once: lay it out at any width later.
Paragraph paragraph = Paragraph.Create(text, style);
Constructors
RichText(string, IEnumerable<StyleSpan>?, IEnumerable<Placeholder>?)
Initializes a new instance of the RichText class.
public RichText(string text, IEnumerable<StyleSpan>? spans = null, IEnumerable<Placeholder>? placeholders = null)
Parameters
textstringThe text.
spansIEnumerable<StyleSpan>Styles over parts of the text, in any order but not overlapping; text outside them takes the paragraph's default style.
placeholdersIEnumerable<Placeholder>One placeholder for each U+FFFC in the text, in order.
Exceptions
- ArgumentNullException
textor a span's style is null.- ArgumentException
A span is empty, outside the text or overlaps another, or the placeholders do not match the text's U+FFFC characters.
Properties
Placeholders
Gets the placeholders, one per U+FFFC in the text, in order.
public IReadOnlyList<Placeholder> Placeholders { get; }
Property Value
Spans
Gets the style spans, sorted by start.
public IReadOnlyList<StyleSpan> Spans { get; }
Property Value
Text
Gets the text.
public string Text { get; }