Creating a Copyright Label Control in .NET MAUI
Environment
Product | Author |
---|---|
.NET MAUI | Dobrinka Yordanova |
Description
I want to know how to create a Copyright label control for my application using .NET MAUI. I need the control to display the copyright symbol followed by a year and some text, with custom text color, underlining, and tap gesture recognizers for handling user interactions.
This knowledge base article also answers the following questions:
- How to format text in a Label using spans in .NET MAUI?
- How to add tap gesture recognizers to a Label in .NET MAUI app?
- How to underline and set text colors for individual spans in .NET MAUI?
Solution
To create a Copyright label control, use the Label
control with formatted text consisting of multiple spans. Below is the implementation:
<Label>
<Label.FormattedText>
<FormattedString>
<!-- Display the copyright symbol -->
<Span Text="©" />
<!-- Display the copyright year and text with custom styling -->
<Span Text="Copyright 2025"
TextColor="Blue"
TextDecorations="Underline">
<!-- Add a tap gesture recognizer -->
<Span.GestureRecognizers>
<TapGestureRecognizer Command="{Binding TapCommand}" />
</Span.GestureRecognizers>
</Span>
</FormattedString>
</Label.FormattedText>
</Label>
- The
Span
insideFormattedString
allows you to define segments of text with different styles and behaviors. - The copyright symbol (
©
) is defined as a separateSpan
. - Another
Span
is used for the "Copyright 2025" text, withTextColor
set toBlue
andTextDecorations
set toUnderline
. - A
TapGestureRecognizer
is added to the secondSpan
to handle user interactions. Bind it to a command in the view model using{Binding TapCommand}
.