Class RichTextBuilder
Builds a RichText: Push(TextStyle) a style, Append(string) text, Pop() back to the enclosing style. Styles nest: a pushed style's unset fields come from the one it is pushed on, and the result has one flat span per stretch of equal style.
public sealed class RichTextBuilder
- Inheritance
-
RichTextBuilder
- 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);
Properties
Depth
Gets the number of styles pushed and not popped.
public int Depth { get; }
Property Value
Length
Gets the length of the text so far, in UTF-16 units.
public int Length { get; }
Property Value
Methods
AddPlaceholder(Placeholder)
Adds an inline box, as a U+FFFC in the current style.
public RichTextBuilder AddPlaceholder(Placeholder placeholder)
Parameters
placeholderPlaceholderThe box.
Returns
- RichTextBuilder
This builder.
Append(ReadOnlySpan<char>)
Appends text in the current style.
public RichTextBuilder Append(ReadOnlySpan<char> text)
Parameters
textReadOnlySpan<char>The text.
Returns
- RichTextBuilder
This builder.
Exceptions
- ArgumentException
The text contains U+FFFC, which only AddPlaceholder(Placeholder) adds.
Append(string)
Appends text in the current style.
public RichTextBuilder Append(string text)
Parameters
textstringThe text.
Returns
- RichTextBuilder
This builder.
Exceptions
- ArgumentException
The text contains U+FFFC, which only AddPlaceholder(Placeholder) adds.
Pop()
Ends the last style pushed.
public RichTextBuilder Pop()
Returns
- RichTextBuilder
This builder.
Exceptions
- InvalidOperationException
No style is pushed.
Push(TextStyle)
Starts a style; text appended until the matching Pop() takes it.
public RichTextBuilder Push(TextStyle style)
Parameters
styleTextStyleThe style; unset fields come from the style it is pushed on.
Returns
- RichTextBuilder
This builder.
Exceptions
- ArgumentNullException
styleis null.
ToRichText()
Builds the rich text; styles still pushed end with the text.
public RichText ToRichText()
Returns
- RichText
The rich text.