|
| 1 | +--- |
| 2 | +title: Creating a Copyright Label Control in .NET MAUI |
| 3 | +description: Learn how to create a Copyright label control using the Label with formatted spans in .NET MAUI application. |
| 4 | +type: how-to |
| 5 | +page_title: How to Use Label with Formatted Spans for a Copyright Control in .NET MAUI |
| 6 | +slug: create-copyright-label-control-dotnet-maui |
| 7 | +tags: label, copyright, formattedtext, span, gesture |
| 8 | +res_type: kb |
| 9 | +--- |
| 10 | + |
| 11 | +## Environment |
| 12 | + |
| 13 | +| Product | Author | |
| 14 | +| --- | ---- | |
| 15 | +| .NET MAUI | [Dobrinka Yordanova](https://www.telerik.com/blogs/author/dobrinka-yordanova) | |
| 16 | + |
| 17 | +## Description |
| 18 | + |
| 19 | +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. |
| 20 | + |
| 21 | +This knowledge base article also answers the following questions: |
| 22 | +- How to format text in a Label using spans in .NET MAUI? |
| 23 | +- How to add tap gesture recognizers to a Label in .NET MAUI app? |
| 24 | +- How to underline and set text colors for individual spans in .NET MAUI? |
| 25 | + |
| 26 | +## Solution |
| 27 | + |
| 28 | +To create a Copyright label control, use the `Label` control with formatted text consisting of multiple spans. Below is the implementation: |
| 29 | + |
| 30 | +```xaml |
| 31 | +<Label> |
| 32 | + <Label.FormattedText> |
| 33 | + <FormattedString> |
| 34 | + <!-- Display the copyright symbol --> |
| 35 | + <Span Text="©" /> |
| 36 | + |
| 37 | + <!-- Display the copyright year and text with custom styling --> |
| 38 | + <Span Text="Copyright 2025" |
| 39 | + TextColor="Blue" |
| 40 | + TextDecorations="Underline"> |
| 41 | + <!-- Add a tap gesture recognizer --> |
| 42 | + <Span.GestureRecognizers> |
| 43 | + <TapGestureRecognizer Command="{Binding TapCommand}" /> |
| 44 | + </Span.GestureRecognizers> |
| 45 | + </Span> |
| 46 | + </FormattedString> |
| 47 | + </Label.FormattedText> |
| 48 | +</Label> |
| 49 | +``` |
| 50 | + |
| 51 | +1. The `Span` inside `FormattedString` allows you to define segments of text with different styles and behaviors. |
| 52 | +2. The copyright symbol (`©`) is defined as a separate `Span`. |
| 53 | +3. Another `Span` is used for the "Copyright 2025" text, with `TextColor` set to `Blue` and `TextDecorations` set to `Underline`. |
| 54 | +4. A `TapGestureRecognizer` is added to the second `Span` to handle user interactions. Bind it to a command in the view model using `{Binding TapCommand}`. |
| 55 | + |
| 56 | +## See Also |
| 57 | + |
| 58 | +- [Label Documentation for .NET MAUI](https://docs.microsoft.com/en-us/dotnet/maui/user-interface/controls/label) |
| 59 | +- [FormattedString Documentation](https://learn.microsoft.com/en-us/dotnet/maui/user-interface/controls/label?view=net-maui-9.0#use-formatted-text) |
| 60 | +- [Gesture Recognizers in .NET MAUI](https://learn.microsoft.com/en-us/dotnet/maui/fundamentals/gestures/tap?view=net-maui-9.0) |
0 commit comments