Text Widget

Render styled text with precise control over formatting and overflow

The Text widget renders text content with programmatic control over styling, justification, and overflow behavior.

When to Use

Use Text when you need programmatic control over styling or when styles are determined at runtime. Common scenarios:

  • Status messages: Apply colors based on success/failure state
  • Computed styles: Build styles dynamically from variables or configuration
  • Container content: Embed styled text inside panels, tables, or other widgets

For inline markup syntax like [bold red]text[/], use Markup instead.

Basic Usage

Create text with a string and optional style.

public static void BasicTextExample()
{
    // Plain text
    var plain = new Text("Hello, World223!");
    AnsiConsole.Write(plain);
    AnsiConsole.WriteLine();
  
    // Text with style in constructor
    var styled = new Text("Styled Text", new Style(foreground: Color.Blue));
    AnsiConsole.Write(styled);
    AnsiConsole.WriteLine();
}

Multi-line Text

Use newline characters (\n) to create multi-line output.

public static void MultiLineTextExample()
{
    var multiLine = new Text("First line\nSecond line\nThird line");
    AnsiConsole.Write(multiLine);
}

Static Members

Use Text.Empty and Text.NewLine for reusable empty text and line break instances.

public static void EmptyTextExample()
{
    AnsiConsole.Write(new Text("Before"));
    AnsiConsole.Write(Text.NewLine);
    AnsiConsole.Write(Text.Empty);
    AnsiConsole.Write(new Text("After"));
}

Justification

Use justification to align text within containers like panels.

public static void TextJustificationExample()
{
    var panel = new Panel(new Rows(
            new Text("Left aligned text").LeftJustified(),
            Text.NewLine,
            new Text("Center aligned text").Centered(),
            Text.NewLine,
            new Text("Right aligned text").RightJustified()
        ))
        .Header("Text Justification")
        .Expand();
  
    AnsiConsole.Write(panel);
}

Set justification via the property when you need to configure it separately from construction.

public static void TextJustificationPropertyExample()
{
    var panel = new Panel(new Rows(
            new Text("Left") { Justification = Justify.Left },
            new Text("Center") { Justification = Justify.Center },
            new Text("Right") { Justification = Justify.Right }
        ))
        .Expand();
  
    AnsiConsole.Write(panel);
}

Overflow

Control what happens when text exceeds available width.

  • Fold - Wraps text to the next line (default)
  • Crop - Truncates text at the boundary
  • Ellipsis - Truncates and adds an ellipsis character (…)
public static void TextOverflowExample()
{
    var longText = "This is a very long piece of text that will demonstrate different overflow behaviors";
  
    var panel = new Panel(new Rows(
        new Markup("[yellow]Fold (wrap):[/]"),
        new Text(longText).Overflow(Overflow.Fold),
        Text.NewLine,
        new Markup("[yellow]Crop (truncate):[/]"),
        new Text(longText).Overflow(Overflow.Crop),
        Text.NewLine,
        new Markup("[yellow]Ellipsis:[/]"),
        new Text(longText).Overflow(Overflow.Ellipsis)
    )) { Width = 40 };
  
    AnsiConsole.Write(panel);
}

Set overflow via the property for separate configuration.

public static void TextOverflowPropertyExample()
{
    var longText = "This is a very long piece of text";
  
    var fold = new Text(longText) { Overflow = Overflow.Fold };
    var crop = new Text(longText) { Overflow = Overflow.Crop };
    var ellipsis = new Text(longText) { Overflow = Overflow.Ellipsis };
  
    var panel = new Panel(new Rows(fold, crop, ellipsis)) { Width = 30 };
  
    AnsiConsole.Write(panel);
}

Styling

Colors

Apply colors to make text stand out or convey meaning.

public static void TextColorsExample()
{
    // Foreground color only
    var foreground = new Text("Blue text", new Style(foreground: Color.Blue));
    AnsiConsole.Write(foreground);
    AnsiConsole.WriteLine();
  
    // Background color only
    var background = new Text("Yellow background", new Style(background: Color.Yellow));
    AnsiConsole.Write(background);
    AnsiConsole.WriteLine();
  
    // Both foreground and background
    var both = new Text("White on Red", new Style(foreground: Color.White, background: Color.Red));
    AnsiConsole.Write(both);
    AnsiConsole.WriteLine();
}

Note

See the Color Reference for all available colors and the Text Style Reference for decoration options.

Decorations

Use decorations to emphasize text: Bold, Italic, Underline, and Strikethrough.

public static void TextDecorationsExample()
{
    var decorations = new[]
    {
        ("Bold", Decoration.Bold), ("Italic", Decoration.Italic), ("Underline", Decoration.Underline),
        ("Strikethrough", Decoration.Strikethrough),
    };
  
    foreach (var (name, decoration) in decorations)
    {
        var text = new Text(name, new Style(decoration: decoration));
        AnsiConsole.Write(text);
        AnsiConsole.WriteLine();
    }
}

Advanced decorations like Dim, Invert, Conceal, and Blink are available but may not work in all terminals.

public static void TextAdvancedDecorationsExample()
{
    var decorations = new[]
    {
        ("Dim", Decoration.Dim), ("Invert", Decoration.Invert), ("Conceal", Decoration.Conceal),
        ("Slow Blink", Decoration.SlowBlink), ("Rapid Blink", Decoration.RapidBlink),
    };
  
    foreach (var (name, decoration) in decorations)
    {
        var text = new Text($"{name} (may not be supported in all terminals)", new Style(decoration: decoration));
        AnsiConsole.Write(text);
        AnsiConsole.WriteLine();
    }
}

Combined Styles

Combine multiple decorations using bitwise flags. Styles can include foreground, background, and decorations together.

public static void TextCombinedStylesExample()
{
    // Multiple decorations combined using flags
    var multiDeco = new Text(
        "Bold, Italic, and Underlined",
        new Style(
            foreground: Color.Green,
            decoration: Decoration.Bold | Decoration.Italic | Decoration.Underline
        )
    );
    AnsiConsole.Write(multiDeco);
    AnsiConsole.WriteLine();
  
    // Full style: foreground, background, and decoration
    var fullStyle = new Text(
        "Complete Style",
        new Style(
            foreground: Color.White,
            background: Color.DarkBlue,
            decoration: Decoration.Bold
        )
    );
    AnsiConsole.Write(fullStyle);
    AnsiConsole.WriteLine();
}

Style Construction

Build styles with the Style constructor for full control, or use Style.Parse() for a compact string syntax.

public static void TextStyleConstructorExample()
{
    // Using Style constructor
    var constructed = new Text(
        "Constructed Style",
        new Style(foreground: Color.Red, decoration: Decoration.Bold)
    );
    AnsiConsole.Write(constructed);
    AnsiConsole.WriteLine();
  
    // Using Style.Parse
    var parsed = new Text("Parsed Style", Style.Parse("bold red"));
    AnsiConsole.Write(parsed);
    AnsiConsole.WriteLine();
}

Properties

Use Length and Lines to inspect text dimensions when building layouts or calculating sizes.

public static void TextPropertiesExample()
{
    var singleLine = new Text("Hello, World!");
    var multiLine = new Text("Line 1\nLine 2\nLine 3");
  
    var table = new Table()
        .AddColumn("Text")
        .AddColumn("Length")
        .AddColumn("Lines");
  
    table.AddRow(
        new Text("\"Hello, World!\""),
        new Text(singleLine.Length.ToString()),
        new Text(singleLine.Lines.ToString())
    );
  
    table.AddRow(
        new Text("\"Line 1\\nLine 2\\nLine 3\""),
        new Text(multiLine.Length.ToString()),
        new Text(multiLine.Lines.ToString())
    );
  
    AnsiConsole.Write(table);
}

Working with Containers

Text widgets work well as content inside panels, tables, and other container widgets.

public static void TextInContainersExample()
{
    // Text in a panel
    var panel = new Panel(
            new Text("This is text inside a panel", new Style(foreground: Color.Yellow))
        )
        .Header("Panel with Text")
        .BorderColor(Color.Blue);
  
    AnsiConsole.Write(panel);
    AnsiConsole.WriteLine();
  
    // Text in a table
    var table = new Table()
        .RoundedBorder()
        .AddColumn("Property")
        .AddColumn("Value");
  
    table.AddRow(
        new Text("Name", new Style(decoration: Decoration.Bold)),
        new Text("Application", new Style(foreground: Color.Green))
    );
  
    table.AddRow(
        new Text("Status", new Style(decoration: Decoration.Bold)),
        new Text("Running", new Style(foreground: Color.Green, decoration: Decoration.Bold))
    );
  
    AnsiConsole.Write(table);
}

API Reference

A renderable piece of text.

Constructors

Text(string text, Style style)

Initializes a new instance of the class.

Parameters:

text (string)
The text.
style (Style)
The style of the text or if .

Properties

Justification : Nullable<Justify>

Gets or sets the text alignment.

Length : int

Gets the character count.

Lines : int

Gets the number of lines in the text.

Overflow : Nullable<Overflow>

Gets or sets the text overflow strategy.

Extension Methods

IEnumerable<Segment> GetSegments(IAnsiConsole console)

Gets the segments for a renderable using the specified console.

Parameters:

console (IAnsiConsole)
The console.

Returns:

An enumerable containing segments representing the specified .