> For the complete documentation index, see [llms.txt](https://docs.flatredball.com/gum/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.flatredball.com/gum/code/controls/label.md).

# Label

## Introduction

Label provides a way to display a string to the user. Unlike TextBox, Labels cannot be changed by typing on the keyboard.

## Code Example: Adding a Label

The following code adds a Label instance to the root:

```csharp
// Initialize
var label = new Label();
label.AddToRoot();
label.Text = "This is a Gum label";
```

[Try on XnaFiddle.NET](https://xnafiddle.net/#snippet=H4sIAAAAAAAAA6tW8ix2L81VsiopKk3VUcrMyyzJTMzJrEpVslIqSyxSyElMSs1RsFXISy1X8AGxNTStY_LAonqOKSkh-UH5-SVIYiGpFSVA5TGlBgZGRiEZmcUKQJSoALQCYhREwlqpFgDtEyD3eQAAAA)

<figure><img src="/files/x6WN6ldhcl3yMIS5mRQD" alt=""><figcaption><p>Label in Gum</p></figcaption></figure>

## Text Wrapping

By default a Label sizes itself to fit its text content, so text does not wrap. To enable wrapping, give the Label a fixed width by setting its `WidthUnits` to `Absolute` and specifying a `Width` value. The height can remain `RelativeToChildren` so the Label grows vertically as needed.

```csharp
// Initialize
var label = new Label();
label.AddToRoot();
label.Text = "This is a long string of text that will wrap to multiple lines when the label has a fixed width.";
label.Visual.Width = 200;
label.Visual.WidthUnits = Gum.DataTypes.DimensionUnitType.Absolute;
label.Visual.HeightUnits = Gum.DataTypes.DimensionUnitType.RelativeToChildren;
```

[Try on XnaFiddle.NET](https://xnafiddle.net/#snippet=H4sIAAAAAAAACo2PQUsEMQyF7wPzH0JPClIWjy4eFhdU8LSMeplLlsluA5l2mGZ2VsX_bjoHEfEgFNJ-7_Ul-agrAPeY76fe3YCOE10thCMro_A7GXYnHEFwTwK3EGmGp3K_uFy3caF-03VN2qWkP1hDZzV765rAGewgSIpHyDqylXQALQ4NqDCzCMwjDqAJ-kmUByEQjpRhDhTNZc9lgIAl6cBn6uxbp8G37rvnC-cJxb8Wbr2vV6s_pWdbLptuS_stKjZvA2W_5Z5i5hSLXJDf7HOSSel3yAPxMeh_U3YkqHyiJt0Flm6kuHZ19VlXX-3xEnx8AQAA)

## Text Alignment

The horizontal and vertical alignment of the text within the Label's bounding box is controlled through the `HorizontalAlignment` and `VerticalAlignment` properties on the `TextComponent`. Cast `TextComponent` to `TextRuntime` to access these properties.

```csharp
// Initialize
var label = new Label();
label.AddToRoot();
label.Text = "Centered text";
label.Visual.Width = 300;
label.Visual.WidthUnits = Gum.DataTypes.DimensionUnitType.Absolute;
label.Visual.Height = 100;
label.Visual.HeightUnits = Gum.DataTypes.DimensionUnitType.Absolute;
var textRuntime = (Gum.GueDeriving.TextRuntime)label.TextComponent;
textRuntime.HorizontalAlignment = RenderingLibrary.Graphics.HorizontalAlignment.Center;
textRuntime.VerticalAlignment = RenderingLibrary.Graphics.VerticalAlignment.Center;
```

[Try on XnaFiddle.NET](https://xnafiddle.net/#snippet=H4sIAAAAAAAACp2QP2vDMBTEd4O_g_CUQBEp3Vo6mBicQroYN128KPHDfiA_GUlOm5R-9z45_ROSDKGbuDv9dLqPOBIieXL50CX3wtsBbkYFCT0qjXtgOdkqK7RagxaPguBNLMN5Mn2oaFRlWtelKYzxR1oJ757jVTIH8mChFp6VKvkNrNANSstXrH3LwbvZ7KL1wk0c-9xQZsqrcteDkxl2QA4NBTtIMl07owcPp5AFYNOGJrfnDxy8f7wQBgnfKQbynOPLk2dDJlcdBEo-QAYWt0jNuMN3bPq3zNx0vSFehmFHILkwFveGvNKpxoa4QqheANXMo2aJa6vsTuZW9S1u3KW8PAx-Al6B9bi5FnuW_oEmcfQZR1--d9KRNgIAAA)

## Auto-Sizing to Text Content

By default a Label's `WidthUnits` and `HeightUnits` are set to `RelativeToChildren`, which means the Label automatically sizes to match its text. When you change `Text`, the Label grows or shrinks to fit the new content with no additional code required.

```csharp
// Initialize
var label = new Label();
label.AddToRoot();
// By default a Label sizes to its text content
label.Text = "Short text";
// Assign longer text and the label grows automatically
label.Text = "This is a much longer string of text";
```

[Try on XnaFiddle.NET](https://xnafiddle.net/#snippet=H4sIAAAAAAAACmWOwUrEQAyG74W-w8-cFMS9K3tYL7LgSXvsJbbZNjBNYCbjuorvbre1JyGQ8PHnS77rCgjH_Fym8ABPhe8WIiouFOWLZxw-KCHSO0fsoXzGy3W-uX1sdaH3h75v7NXMF7bb4emCnk9UooPWNPLsynCD-Nz409GZOqtvkubK9mjD22jJl0gbVt0hZxkU0XTgtC6T9vCR_94akp0zqLhN5NJRjJd_2maUjLkIU-nGTZY9iQ6w03Yw1NVPXf0CT0_REBgBAAA)

To constrain a Label to a fixed size and prevent it from growing with its content, set `WidthUnits` to `Absolute` and assign an explicit `Width`.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.flatredball.com/gum/code/controls/label.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
