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
Labelin 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
Labelin aStackLayout. TheLabel.Textproperty specifies the text that is displayed, and theHorizontalOptionsproperty specifies that theLabelwill 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
Labeldeclaration 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. TheTextColorproperty sets the color of theLabeltext. TheFontAttributesproperty sets the font for the label to italic, and theFontSizeproperty sets the font size. In addition, an underline text decoration is applied to theLabelby setting itsTextDecorationsproperty, and it's horizontally centered by setting theHorizontalOptionsproperty 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
Labelappearance has changed:For more information about setting
Labelappearance, see the Xamarin.Forms Label guide.
Present formatted text
In MainPage.xaml, modify the
Labeldeclaration 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 firstSpanis displayed using the formatting set on theLabel, while the text in the second and thirdSpaninstances is displayed using the formatting set on theLabeland the additional formatting set on eachSpan.Note
The
FormattedTextproperty is of typeFormattedString, which comprises one or moreSpaninstances.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
Labelappearance has changed:In Visual Studio, stop the application.
For more information about setting
Spanappearance, see Formatted text in the Xamarin.Forms Label guide.
Tebrikler!
Congratulations on completing this tutorial, where you learned how to:
- Create a Xamarin.Forms
Labelin 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
Bu bölümle ilgili bir sorununuz mu var? Öyleyse bu bölümü iyileştirebilmemiz için lütfen geri bildirimde bulunun.


