Xamarin.Forms - Text App

Introduction

This article demonstrates the various views for displaying and entering text available in Xamarin.Forms.The app consists of several pages, demonstrating each of the text controls and a few common text entry scenarios.
               
It has three views for working with text 
  • Label - single or multiple line text
  • Entry - only one line input
  • Editor - more than one line input.

Let's start :)

Step 1

You can create Xamarin.Forms App by going to File >>New >> Visual C# >> Cross Platform >>  Cross Platform App (Xamarin.Native or Xamarin.Forms), enter desired name for your project and press ok.

(ProjectName = TextApp)

 
 
Step 2

Open Solution Explorer >> TextApp(PCL) >> right click and select Add >> New Item. In the popup window select Cross Platform >> Empty content page Xaml.

In this way, you can add new Xaml page.Create a new Xaml page named LabePage.Xaml and double click to open its design view and insert the code is given below.

Label 

The Label  have cutom fonts and color text.
  •  Truncating and Wrapping.
  •   Font .
  •   Color -  set to use a custom text color.
  •    Formatted Text.
Formatted Text
  • BackgroundColor - set a background color 
  • FontAttributes - set to Bold, italic or none
  • FontFamily - Sets the Font to be used
  • FontSize - Sets the size of the font
  • Text -  Display a text
Fonts
  • FontFamily -  sets font to be used
  • FontSize
    • Micro
    • Small
    • Medium
    • Large
  • FontAttributes
    • None
    • Italic
    • Bold
  • FormattedString
    • Text
    • FontFamily
    • FontSize
  • FontAttributes
    • ForGroundColor
    • BackgroundColor

Label Page

The label is used to display text and it displays mulitiple or singleline text. The label view can truncate text when it can't fit on one line. 

Used ToolBox Items

  •  Label - color label
  •  Label - This is default, non customized label
  •  Label - FontSize label
  •  Label - FontAttributes label
  •  Label - FormattedString label
 Xaml Code
  1. <?xml version="1.0" encoding="utf-8" ?>  
  2. <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"  
  3.              xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"  
  4.              x:Class="TextApp.LabePage"  
  5.              Title="Label Page">  
  6.     <ContentPage.Content>  
  7.         <StackLayout Padding="5,10" x:Name="layout">  
  8.             <Label TextColor="Green" Text="This is green Label"/>  
  9.             <Label Text="This is default , non custumized label"/>  
  10.             <Label FontSize="30" Text="This is Font Size label"/>  
  11.             <Label FontAttributes="Bold" Text="This is bold text Label"/>  
  12.             <Label>  
  13.                 <Label.FormattedText>  
  14.                     <FormattedString>  
  15.                         <Span Text="Red color Bold" ForegroundColor="Red" FontAttributes="Bold"/>  
  16.                         <Span Text="Default"/>  
  17.                         <Span Text="italic small" FontAttributes="Italic" FontSize="Large"/>  
  18.                     </FormattedString>  
  19.                 </Label.FormattedText>  
  20.             </Label>  
  21.         </StackLayout>  
  22.     </ContentPage.Content>  
  23. </ContentPage> 
Result

Step 3 - Editor Page

This page demonstrates the editor view.The Editor is used for collecting text that is expected to take  more than one line.

In this step, Add another a Xaml page named Editor page. Here the code for this page.
 
Xaml Code

used Toolbox items
  • Label - Display text
  • Editor - Background and HeightRequest Editor
  • Editor - Default editor
  • Editor - Disbled editor
  1. <?xml version="1.0" encoding="utf-8" ?>  
  2. <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"  
  3.              xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"  
  4.              x:Class="TextApp.EditorPage"  
  5.              Title="Editor Page">  
  6.     <ContentPage.Content>  
  7.         <StackLayout Padding="5,10">  
  8.             <Label Text="This Editor View Page"/>  
  9.             <Editor Text="Xamarin.Forms" BackgroundColor="Maroon" HeightRequest="100"/>  
  10.             <Editor Text="Default starting Page"/>  
  11.             <Editor IsEnabled="False" Text="This is disbled Editor"/>  
  12.         </StackLayout>  
  13.     </ContentPage.Content>  
  14. </ContentPage> 
Result


 
 
Step 4 - Entry 

Entry is used to accept single line text input but cannot have customized fonts.The Entry has a password mode and can show placeholder text until text is entered.

Entry Page

This page demonstrates the Entry view. The Entry is used for collecting text that is expected to take one line. Unlike the Editor, the Entry view supports more advanced formatting and customization.
 
Now, Create a Xaml page named Entrypage.Xaml and here is  the code

Xaml Code
  •  Entry - Textcolor entry
  •  Entry - Placeholder and Background color entry
  •  Entry - Password entry
  •  Entry - default entry
  •  Entry - Placeholder entry
  •  Entry - Disbled entry
  •  Entry - Textcolor entry
  •  StackLayout
  •  ScrollView
  1. <?xml version="1.0" encoding="utf-8" ?>  
  2. <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"  
  3.              xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"  
  4.              x:Class="TextApp.EntryPage"  
  5.              Title="Entry Page">  
  6.     <ContentPage.Content>  
  7.         <StackLayout Padding="5,10">  
  8.             <Label Text="This page is Entry View"/>  
  9.             <Label Text="Unlike the Editor, the Entry View supprts more Editor Views"/>  
  10.             <ScrollView VerticalOptions="StartAndExpand">  
  11.                 <StackLayout>  
  12.                     <Entry Text="Xamarin.Forms" TextColor="Fuchsia"/>  
  13.                     <Entry Placeholder="UserName" BackgroundColor="Gray"/>  
  14.                     <Entry Placeholder="Password" IsPassword="True"/>  
  15.                     <Entry Text="Default Prsentation"/>  
  16.                     <Entry Placeholder="Placeholder Text"/>  
  17.                     <Entry IsEnabled="False" Text="This is disbled Entry"/>  
  18.                     <Entry TextColor="Olive" Text="This is an entry with Text View"/>  
  19.                 </StackLayout>  
  20.             </ScrollView>  
  21.         </StackLayout>  
  22.     </ContentPage.Content>  
  23. </ContentPage> 
Result


 
Step 5 - Login Page

This login page has two types of login frame.
  •   Treditional login form
  •    Modern login form

Xaml Code

  •  Grid
  •  Label
  •  Frame
  •  StackLayout
  •  Button
  •  Entry
  1. <?xml version="1.0" encoding="utf-8" ?>  
  2. <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"  
  3.              xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"  
  4.              x:Class="TextApp.LoginPage"  
  5.              Title="Login Page">  
  6.   
  7.     <ContentPage.Content>  
  8.         <StackLayout Padding="10,0">  
  9.             <Label FontSize="Large" HorizontalOptions="Center" Text="Traditional Login Form" />  
  10.             <Frame>  
  11.                 <Grid HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand">  
  12.                     <Grid.RowDefinitions>  
  13.                         <RowDefinition Height="50" />  
  14.                         <RowDefinition Height="50" />  
  15.                         <RowDefinition Height="50" />  
  16.                     </Grid.RowDefinitions>  
  17.                     <Grid.ColumnDefinitions>  
  18.                         <ColumnDefinition Width="80" />  
  19.                         <ColumnDefinition Width="*" />  
  20.                     </Grid.ColumnDefinitions>  
  21.                     <Label Text="Username" FontSize="Small" FontAttributes="Bold" Grid.Row="0" Grid.Column="0" VerticalOptions="Center" />  
  22.                     <Label Text="Password" FontSize="Small" FontAttributes="Bold" Grid.Row="1" Grid.Column="0" VerticalOptions="Center" />  
  23.                     <Entry Grid.Row="0" Grid.Column="1" />  
  24.                     <Entry Grid.Row="1" Grid.Column="1" IsPassword="true" />  
  25.                     <Button Text="Log In" TextColor="White" Grid.Row="2" Grid.Column="1" BackgroundColor="Gray" />  
  26.                 </Grid>  
  27.             </Frame>  
  28.             <Label FontSize="Large" HorizontalOptions="Center" Text="Modern Login Form" />  
  29.             <Frame>  
  30.                 <StackLayout>  
  31.                     <Entry Placeholder="Username" HorizontalOptions="Fill" />  
  32.                     <Entry Placeholder="Password" IsPassword="true" HorizontalOptions="Fill" />  
  33.                     <Button Text="Log In" TextColor="White"  BackgroundColor="Gray" HorizontalOptions="Fill" />  
  34.                 </StackLayout>  
  35.             </Frame>  
  36.         </StackLayout>  
  37.     </ContentPage.Content>  
  38.   
  39. </ContentPage> 
Result


 
 
Step 6 - Order Page

This page demonstrates order system view.

Next, add a new Xaml page named Orderpage.xaml and click open OrderPage.xaml and This page code is here.
 
Xaml Code 

Used toolbox items
  •  Button
  •  Entry
  •  Editor
  •  Label
  •  Grid
  •  FormattingText
  1. <?xml version="1.0" encoding="utf-8" ?>  
  2. <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"  
  3.              xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"  
  4.              x:Class="TextApp.OrderPage"  
  5.              Title="Order Page">  
  6.     <ContentPage.Content>  
  7.         <ScrollView>  
  8.             <Grid HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand" Padding="15">  
  9.                 <Grid.RowDefinitions>  
  10.                     <RowDefinition Height="50" />  
  11.                     <RowDefinition Height="50" />  
  12.                     <RowDefinition Height="50" />  
  13.                     <RowDefinition Height="50" />  
  14.                     <RowDefinition Height="50" />  
  15.                     <RowDefinition Height="50" />  
  16.                     <RowDefinition Height="50" />  
  17.                 </Grid.RowDefinitions>  
  18.                 <Grid.ColumnDefinitions>  
  19.                     <ColumnDefinition Width="90" />  
  20.                     <ColumnDefinition Width="*" />  
  21.                 </Grid.ColumnDefinitions>  
  22.                 <Label Text="Purchaser Name:" Grid.Column="0" Grid.Row="0" />  
  23.                 <Entry Placeholder="Full Name on Card" Grid.Column="1" Grid.Row="0" />  
  24.                 <Label Text="Billing Address:" Grid.Column="0" Grid.Row="1" />  
  25.                 <Editor Grid.Column="1" Grid.Row="1" />  
  26.                 <Label Text="Tip:" FontAttributes="Bold" Grid.Column="0" Grid.Row="2"  />  
  27.                 <Entry Keyboard="Numeric" Grid.Row="2" Grid.Column="1" />  
  28.                 <Label Text="Phone Number:" Grid.Column="0" Grid.Row="3" />  
  29.                 <Entry Keyboard="Telephone" Grid.Row="3" Grid.Column="1" />  
  30.                 <Label Text="Comments:" Grid.Column="0" Grid.Row="4" />  
  31.                 <Editor Grid.Column="1" Grid.Row = "4" />  
  32.                 <Label Grid.Column="1" Grid.Row="5">  
  33.                     <Label.FormattedText>  
  34.                         <FormattedString>  
  35.                             <FormattedString.Spans>  
  36.                                 <Span Text="Wait! " ForegroundColor="Red" />  
  37.                                 <Span Text="Please double check that everything is right." />  
  38.                             </FormattedString.Spans>  
  39.                         </FormattedString>  
  40.                     </Label.FormattedText>  
  41.                 </Label>  
  42.                 <Button Text="Save" TextColor="White" Grid.Row="6" Grid.Column="1" BackgroundColor="Gray" />  
  43.             </Grid>  
  44.         </ScrollView>  
  45.     </ContentPage.Content>  
  46. </ContentPage> 
Result
 
 
Step 7 - Style 
  • Built in style 
  • Built in style for common scenarios
    • BodyStyle
    • CaptionStyle
    • ListItemDetailTextStyle
    • ListItemTextStyle
    • SubtitleStyle
    • TitleStyle 
  • Custom Style
  • The style consist of setter and setters consist of properties
  •  Applying Style
  • "TargetType"  - once the style has been created, it can be applied to any view
    • AccessiblitY

Built in style page

This page demonstrates the Built in Style  view. The Built in Style  is used to display text in already built styles.

It use DynamicResources for display style text.

Now,Add another Xaml Page named Builtinstylepage.xaml and double click open Builtinstylepage.xaml and here the code. 
 
Xaml page
  •  Label
  •  TitleStyle
  •  SubtitleStyle
  •  BodyStyle
  •  CaptionStyle
  •  ListItemStyle
  •  ListItemDetailTextStyle
  •  StackLayout
  1. <?xml version="1.0" encoding="utf-8" ?>  
  2. <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"  
  3.              xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"  
  4.              x:Class="TextApp.BuiltinStyle"  
  5.              Title="Built in Style Page ">  
  6.     <ContentPage.Content>  
  7.         <StackLayout Spacing="10" Padding="10">  
  8.             <Label Text="Title Style" Style="{DynamicResource TitleStyle}" />  
  9.             <Label Text="Subtitle Style" Style="{DynamicResource SubtitleStyle}" />  
  10.             <Label Text="Body Style" Style="{DynamicResource BodyStyle}" />  
  11.             <Label Text="Caption Style" Style = "{DynamicResource CaptionStyle}" />  
  12.             <Label Text="ListItemText Style" Style="{DynamicResource ListItemTextStyle}" />  
  13.             <Label Text="ListItemDetailText Style" Style="{DynamicResource ListItemDetailTextStyle}" />  
  14.         </StackLayout>  
  15.     </ContentPage.Content>  
  16. </ContentPage> 
Result


 
 
Step 8 - Custom style page

This page demonstrates the Custom Style view. The custom style is used to display text in different custom styles. It uses StaticResources  and Setter Properties.

In this step, add new Xaml page named CustomStylePage and this page code is given below.

Xaml Code

Used toolbox items
  •  Style
  •  Setter and properties
  •  StackLayout
  •  Label
  1. <?xml version="1.0" encoding="utf-8" ?>  
  2. <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"  
  3. 9/xaml"  
  4.              x:Class="TextApp.CustumStyle"  
  5.              Title="Custum Stye Page">  
  6.     <ContentPage.Resources>  
  7.              xmlns:x="http://schemas.microsoft.com/winfx/200  
  8.         <ResourceDictionary>  
  9.             <Style x:Key="LabelStyle" TargetType="Label">  
  10.                 <Setter Property="TextColor" Value="Red"/>  
  11.                 <Setter Property="FontSize" Value="30"/>  
  12.             </Style>  
  13.         </ResourceDictionary>  
  14.     </ContentPage.Resources>  
  15.     <ContentPage.Content>  
  16.         <StackLayout Padding="15" Spacing="15">  
  17.             <Label Text="Check out my style." Style="{StaticResource LabelStyle}" />  
  18.         </StackLayout>  
  19.     </ContentPage.Content>   
  20. </ContentPage> 
Result


 

Step 9 - Editor

The editor is used to accept multiple line text input. It has custom background color, but text color and font cannot be changed.
  •  Customization - color and keyboard options
    • Default keyboard
    • Chat Keyboard
    • Email Keyboard
    • Numeric Keyboard
    • Telephone Keyboard
    • Url - web addresses
  • Interactivity - it exposes two events
    • TextChanged -  when the text changes in the Editor
    • Complated - when the user ended input by pressing the return key on the keyboard

In this step, add new Xaml page named Editorpage.xaml and double click to get it's design view and here the code for this page

Xaml Code

Used toolbox items
  1.  Label
  2.  Editor - Backgroundcolor and Heightreguest editor
  3.  Editor - default editor
  4.  Editor - Disabled editor
  1. <?xml version="1.0" encoding="utf-8" ?>  
  2. <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"  
  3.              xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"  
  4.              x:Class="TextApp.EditorPage"  
  5.              Title="Editor Page">  
  6.     <ContentPage.Content>  
  7.         <StackLayout Padding="5,10">  
  8.             <Label Text="This Editor View Page"/>  
  9.             <Editor Text="Xamarin.Forms" BackgroundColor="Maroon" HeightRequest="100"/>  
  10.             <Editor Text="Default starting Page"/>  
  11.             <Editor IsEnabled="False" Text="This is disbled Editor"/>  
  12.         </StackLayout>  
  13.     </ContentPage.Content>  
  14. </ContentPage>     
Result


 
 
 
Step 10

In this step, go to Solution Explorer >>TextApp (PCL) >> click open MainPage.xaml page and find the code.

Xaml Code

Used toolbox items
  •  ListView
  •  DataTemplate
  •  TextCell
  •  Binding property
  1. <?xml version="1.0" encoding="utf-8" ?>  
  2. <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"  
  3.              xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"  
  4.              xmlns:local="clr-namespace:TextApp"  
  5.              x:Class="TextApp.MainPage"  
  6.              Title="MainPage">  
  7.     <ContentPage.Content>  
  8.         <ListView x:Name="ListOfPages" ItemSelected="ListOfPages_ItemSelected">  
  9.             <ListView.ItemTemplate>  
  10.                 <DataTemplate>  
  11.                     <TextCell Text="{Binding Title}" TextColor="Navy"/>  
  12.                 </DataTemplate>  
  13.             </ListView.ItemTemplate>  
  14.         </ListView>  
  15.     </ContentPage.Content>  
  16. </ContentPage> 
Next, open Solution Explorer >> TextApp(PCL) >> MainPage.xaml.cs >> click open MainPage.xaml.cs.Here the code for this class
 
cs code
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6. using Xamarin.Forms;  
  7.   
  8. namespace TextApp  
  9. {  
  10.     public partial class MainPage : ContentPage  
  11.     {  
  12.         public MainPage()  
  13.         {  
  14.             InitializeComponent();  
  15.             List<Page> pages = new List<Page>();  
  16.             pages.Add(new EditorPage());  
  17.             pages.Add(new EntryPage());  
  18.             pages.Add(new LabePage());  
  19.             pages.Add(new LoginPage());  
  20.             pages.Add(new OrderPage());  
  21.             pages.Add(new BuiltinStyle());  
  22.             pages.Add(new CustumStyle());  
  23.             ListOfPages.ItemsSource = pages;  
  24.         }  
  25.   
  26.         private void ListOfPages_ItemSelected(object sender, SelectedItemChangedEventArgs e)  
  27.         {  
  28.             if (e.SelectedItem != null)  
  29.             {  
  30.                 this.Navigation.PushAsync((Page)e.SelectedItem);  
  31.             }  
  32.             ListOfPages.SelectedItem = null;  
  33.   
  34.         }  
  35.     }  

Step 11

After the design view, go to Solution Explorer >> TextApp(PCL) >> click open App.xaml.cs and this page code is given below.

cs code
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5.   
  6. using Xamarin.Forms;  
  7.   
  8. namespace TextApp  
  9. {  
  10.     public partial class App : Application  
  11.     {  
  12.         public App()  
  13.         {  
  14.             InitializeComponent();  
  15.   
  16.             MainPage = new NavigationPage();  
  17.             MainPage.Navigation.PushAsync(new MainPage());  
  18.         }  
  19.   
  20.         protected override void OnStart()  
  21.         {  
  22.             // Handle when your app starts  
  23.         }  
  24.   
  25.         protected override void OnSleep()  
  26.         {  
  27.             // Handle when your app sleeps  
  28.         }  
  29.   
  30.         protected override void OnResume()  
  31.         {  
  32.             // Handle when your app resumes  
  33.         }  
  34.     }  
  35.  
Step 12

Click F5 or Build to Run your projects.Running this project, you will have the results like below.

 
 
Finally, we have successfully created  a Xamarin.Forms TextApp Application.


Similar Articles