| MAUI-Controls :👈 | 👉:MAUI-Entry |
.NET MAUI Label Control |
.NET MAUI Label Control
In .NET MAUI, the Label control is a fundamental user interface element used to display single-line or multi-line read-only text. It is highly customizable, allowing developers to control its appearance through various properties and support for advanced formatting.
Key Features and Capabilities
Text Formatting: You can set basic text using the Text property or use the FormattedText property with Span objects to apply different colors, fonts, and styles to specific segments of the text.
HTML Support: By setting the TextType property to Html, the Label can render a subset of HTML tags (e.g., <b>, <i>, <u>, and basic block-level elements), though support varies by platform.
Multi-line and Truncation
MaxLines: Limits the number of lines displayed.
LineBreakMode: Determines how text is handled when it exceeds the Label's width (e.g., TailTruncation, HeadTruncation, MiddleTruncation, NoWrap, or WordWrap).
Alignment and Spacing: Properties like HorizontalTextAlignment, VerticalTextAlignment, LineHeight, and CharacterSpacing provide fine-grained control over text positioning.
Text Transformation: The TextTransform property allows for automatic casing changes, such as Uppercase, Lowercase, or None.
Essential Properties
| Property | Type | Description |
|---|---|---|
| Text | string | The plain text to be displayed. |
| TextColor | Color | The color of the text. |
| FontSize | double | The size of the font. |
| FontFamily | string | The font family name. |
| FontAttributes | Enum | Sets text to Bold, Italic, or None. |
| TextDecorations | Enum | Adds Underline or Strikethrough. |
π What is a Label?
A Label is a read-only text control used to display static or dynamic text in your app. Itβs lightweight, flexible, and supports styling, formatting, and data binding.
π Key Features
Text Display: Shows plain or formatted text.
Styling: Supports font size, color, weight, and family.
Text Alignment: Horizontal and vertical alignment options.
Line Breaks & Wrapping: Control text wrapping and truncation.
FormattedString: Combine multiple Span elements for rich text (different styles in one label).
Data Binding: Bind text to ViewModel properties for dynamic updates.
Accessibility: Works with screen readers and semantic properties.
π Example Code
Simple Label
xml
<Label Text="Hello, .NET MAUI!"
FontSize="24"
TextColor="DarkBlue"
HorizontalOptions="Center"
VerticalOptions="Center"/>
Formatted Label
xml
<Label>
<Label.FormattedText>
<FormattedString>
<Span Text="Welcome " FontSize="20" TextColor="Black"/>
<Span Text="Shivshanker" FontAttributes="Bold" TextColor="DarkRed"/>
</FormattedString>
</Label.FormattedText>
</Label>
β Why Choose Label?
Lightweight: Perfect for displaying text without interactivity.
Flexible Styling: Easily customize fonts, colors, and alignment.
Data Binding: Ideal for showing dynamic text from your ViewModel.
Rich Text Support: Use FormattedString for multi-style text in one control.
π Common Use Cases
Headings & Titles: Page headers, section titles.
Instructions: Static text guiding users.
Dynamic Data: Display bound values (e.g., user name, status).
Rich Text: Highlight parts of text differently (e.g., bold keywords).
Labels in Forms: Field names or descriptions.
β οΈ Considerations
Non-interactive: Labels donβt handle input; use Entry or Editor for editable text.
Performance: Very efficient, but for large text blocks consider Editor or ScrollView.
β¨ In short: The MAUI Label is your go-to control for displaying text β simple, styled, or richly formatted. Itβs lightweight, flexible, and integrates seamlessly with data binding.
Hereβs a quick comparison table of Label vs Entry vs Editor in .NET MAUI β a handy cheat sheet to decide which text control fits your scenario best:
βοΈ Label vs Entry vs Editor
| Control | Purpose | Input Type | Key Features | Best Use Cases |
|---|---|---|---|---|
| Label | Display text only | β No input |
- Read-only text - Supports FormattedString for rich text - Lightweight and fast |
Titles, headings, instructions, dynamic data display |
| Entry | Single-line text input | β Yes (single line) |
- Placeholder support - Password mode - Keyboard customization - Events (Completed, TextChanged) |
Login forms, search bars, short fields (username, email, phone) |
| Editor | Multi-line text input | β Yes (multi-line) |
- Text wrapping - Built-in scrolling - Placeholder support - Events (TextChanged, Focused) |
Comments, feedback forms, notes, descriptions, messaging apps |
β Quick Decision Guide
Need to show text only? β Use Label.
Need short, single-line input? β Use Entry.
Need long, multi-line input? β Use Editor.
β¨ In short:
Label = display-only.
Entry = short input.
Editor = long input.
| MAUI-Controls :👈 | 👉:MAUI-Entry |