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:

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

Try on XnaFiddle.NETarrow-up-right

Label in Gum

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.

Try on XnaFiddle.NETarrow-up-right

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.

Try on XnaFiddle.NETarrow-up-right

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.

Try on XnaFiddle.NETarrow-up-right

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.

Last updated

Was this helpful?