xamarin
  1. xamarin-form-labels

Xamarin Forms Labels

Labels are one of the oldest, but essential widgets used to display text in a Xamarin Forms application. They are commonly used to display a single string of text or multiple lines of text. The Xamarin Forms label control is simple, flexible, and customizable and can be used to display different types of text such as Titles, Text Messages, Descriptions, etc.

Syntax

The following XAML code shows how to create a label control in a Xamarin Forms application.

<Label Text="Hello World!" 
       FontSize="Medium" 
       HorizontalOptions="Center" 
       VerticalOptions="Center" />

Example

Here is an example of how to create a label in a Xamarin Forms app:

<StackLayout>
   <Label Text="Hello Xamarin Forms!" 
          FontSize="Large" 
          FontAttributes="Bold" 
          HorizontalOptions="Center" 
          VerticalOptions="Center" />
</StackLayout>

Output

The above XAML creates a label with the Text "Hello Xamarin Forms!" and shown on the center of the screen in the Large font size.

Explanation

The code above uses the <Label> element to create a label control. The Text property is used to set the label's text, while the FontSize property is used to set the font size of the text. The FontAttributes property is used to set the font weight of the text. In the example above, the text is set to be bold. The HorizontalOptions and VerticalOptions properties align the label horizontally and vertically in the center of the screen.

Use

Labels can be used in various situations in a Xamarin Forms application. Here are some of the use cases:

  • To display informative text that provides feedback to the user
  • To display text-based titles, headers, and sub-headers
  • To display descriptions of products, features, and images
  • To display error messages, warnings, and alerts
  • To display instructions and guidelines

Important Points

  • The Xamarin Forms Label widget is a simple, flexible, and customizable control.
  • The Label control can be used to display different types of text such as Titles, Text Messages, Descriptions, etc.
  • The <Label> element can be used in XAML markup to create a label control and apply properties.
  • The Text property is used to set the label's text, while the FontSize and FontAttributes properties set the font size and font weight of the text.
  • The HorizontalOptions and VerticalOptions properties align the label horizontally and vertically in the center of the screen.

Summary

Labels are an essential widgets used to display text in a Xamarin Forms application. They are commonly used to display a single string of text or multiple lines of text, such as titles, descriptions, messages, and much more. The Xamarin Forms label control is simple, flexible, customizable, and can be used to display different types of text.

Published on: