Label Control
The Label
control is a UI element used to display a single line or multiple lines of read-only text. It's a fundamental control for presenting information to the user.
Overview
The Label
control provides a simple way to display text. You can control its appearance through various properties such as font, color, size, and alignment. It also supports multi-line text, text wrapping, and character spacing.
Basic Usage
A basic Label
can be created in XAML by setting its Text
property:
<Label Text="Hello, .NET MAUI!" />
Or in C#:
var label = new Label
{
Text = "Hello, .NET MAUI!"
};
Key Properties
Property | Description | Default Value |
---|---|---|
Text |
The text to display. | Empty string. |
TextColor |
The color of the text. | System default (usually black). |
FontSize |
The size of the font. | System default. |
FontAttributes |
Specifies font formatting such as bold, italic, or combined. | FontAttributes.None |
FontFamily |
The font family to use. | System default. |
HorizontalTextAlignment |
The horizontal alignment of the text within the label. | TextAlignment.Start |
VerticalTextAlignment |
The vertical alignment of the text within the label. | TextAlignment.Start |
LineBreakMode |
Specifies how text should be truncated or wrapped. | LineBreakMode.None |
MaxLines |
The maximum number of lines the label can display. | -1 (unlimited). |
CharacterSpacing |
The spacing between characters. | 0 |
Common Scenarios
Multi-line Text and Wrapping
To display multiple lines of text, simply include newline characters (\n
) in the Text
property. The Label
will automatically adjust its height. To control how text wraps or is truncated, use the LineBreakMode
property.
<Label Text="This is a long piece of text that needs to wrap onto multiple lines.
It demonstrates the automatic wrapping behavior of the Label control."
LineBreakMode="WordWrap"
FontSize="18" />
Text Alignment
You can center text horizontally and vertically within the label:
<Label Text="Centered Text"
HorizontalTextAlignment="Center"
VerticalTextAlignment="Center"
HeightRequest="100" />
Font Styling
Customize the font appearance:
<Label Text="Bold and Italic"
FontSize="22"
FontAttributes="Bold, Italic"
TextColor="DarkGreen" />
Platform Considerations
The Label
control renders natively on each platform, ensuring a consistent look and feel. Most properties have direct mappings to native UI text elements.
For example, HorizontalTextAlignment
maps to platform-specific text alignment behaviors.
API Reference
For a complete list of properties, methods, and events, please refer to the official API documentation: