Xamarin.Forms Label Tutorial
Before attempting this tutorial, you should have successfully completed the:
- Build your first Xamarin.Forms app quickstart.
- StackLayout tutorial.
In this tutorial, you learn how to:
- Create a Xamarin.Forms
Label
in XAML. - Change the appearance of the
Label
. - Present text, in a single
Label
, that has multiple formats.
You will use Visual Studio 2019, or Visual Studio for Mac, to create a simple application that demonstrates how to display text in a Label
. The following screenshots show the final application:
You'll also use XAML Hot Reload for Xamarin.Forms to see UI changes without rebuilding your application.
Create a label
To complete this tutorial you should have Visual Studio 2019 (latest release), with the Mobile development with .NET workload installed. In addition, you will require a paired Mac to build the tutorial application on iOS. For information about installing the Xamarin platform, see Installing Xamarin. For information about connecting Visual Studio 2019 to a Mac build host, see Pair to Mac for Xamarin.iOS development.
Launch Visual Studio, and create a new blank Xamarin.Forms app named LabelTutorial.
Important
The C# and XAML snippets in this tutorial requires that the solution is named LabelTutorial. Using a different name will result in build errors when you copy code from this tutorial into the solution.
For more information about the .NET Standard library that gets created, see Anatomy of a Xamarin.Forms application in the Xamarin.Forms Quickstart Deep Dive.
In Solution Explorer, in the LabelTutorial project, double-click MainPage.xaml to open it. Then, in MainPage.xaml, remove all of the template code and replace it with the following code:
<?xml version="1.0" encoding="utf-8"?> <ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="LabelTutorial.MainPage"> <StackLayout Margin="20,35,20,20"> <Label Text="Welcome to Xamarin.Forms!" HorizontalOptions="Center" /> </StackLayout> </ContentPage>
This code declaratively defines the user interface for the page, which consists of a
Label
in aStackLayout
. TheLabel.Text
property specifies the text that is displayed, and theHorizontalOptions
property specifies that theLabel
will be horizontally centered.In the Visual Studio toolbar, press the Start button (the triangular button that resembles a Play button) to launch the application inside your chosen remote iOS simulator or Android emulator:
Change appearance
In MainPage.xaml, modify the
Label
declaration to change its visual appearance:<Label Text="Welcome to Xamarin.Forms!" TextColor="Blue" FontAttributes="Italic" FontSize="22" TextDecorations="Underline" HorizontalOptions="Center" />
This code sets properties that change the visual appearance of the
Label
. TheTextColor
property sets the color of theLabel
text. TheFontAttributes
property sets the font for the label to italic, and theFontSize
property sets the font size. In addition, an underline text decoration is applied to theLabel
by setting itsTextDecorations
property, and it's horizontally centered by setting theHorizontalOptions
property toCenter
.If the application is still running, save the changes to the file and the application user interface will automatically be updated in your simulator or emulator. Otherwise, in the Visual Studio toolbar, press the Start button (the triangular button that resembles a Play button) to launch the application inside your chosen remote iOS simulator or Android emulator. Observe that the
Label
appearance has changed:For more information about setting
Label
appearance, see the Xamarin.Forms Label guide.
Present formatted text
In MainPage.xaml, modify the
Label
declaration to present text that uses multiple formats, in a singleLabel
.<Label TextColor="Gray" FontSize="Medium"> <Label.FormattedText> <FormattedString> <Span Text="This sentence contains " /> <Span Text="words that are emphasized, " FontAttributes="Italic" /> <Span Text="and underlined." TextDecorations="Underline" /> </FormattedString> </Label.FormattedText> </Label>
This code displays text, in a single
Label
, that uses multiple formats. The text in the firstSpan
is displayed using the formatting set on theLabel
, while the text in the second and thirdSpan
instances is displayed using the formatting set on theLabel
and the additional formatting set on eachSpan
.Note
The
FormattedText
property is of typeFormattedString
, which comprises one or moreSpan
instances.If the application is still running, save the changes to the file and the application user interface will automatically be updated in your simulator or emulator. Otherwise, in the Visual Studio toolbar, press the Start button (the triangular button that resembles a Play button) to launch the application inside your chosen remote iOS simulator or Android emulator. Observe that the
Label
appearance has changed:In Visual Studio, stop the application.
For more information about setting
Span
appearance, see Formatted text in the Xamarin.Forms Label guide.
Congratulations!
Congratulations on completing this tutorial, where you learned how to:
- Create a Xamarin.Forms
Label
in XAML. - Change the appearance of the
Label
. - Present text, in a single
Label
, that has multiple formats.
Next steps
To learn more about the basics of creating mobile applications with Xamarin.Forms, continue to the Button tutorial.
Related links
Have an issue with this section? If so, please give us some feedback so we can improve this section.