Xamarin.Forms Custom Underline Label

I came under the task of underline on a label in the design of a login and I also needed the underline label, that's why I have created a custom underline label today.

So let's start friends,

Firstly we create a ContentView whose name is under UnderLineLabel.xaml and then set the Label and Boxview.

  1. <ContentView.Content>  
  2.        <StackLayout  Grid.Row="0" Padding="0" VerticalOptions="Center">  
  3.            <Label x:Name="customLabel" Text="{Binding Text, Mode=TwoWay}" TextColor="{Binding TextColor,Mode=TwoWay}"/>  
  4.            <BoxView x:Name="customBox"   
  5.                     BackgroundColor="{Binding LineColor,Mode=TwoWay}"  
  6.                     HeightRequest="1"   
  7.                     Margin="0,-8,0,0" />  
  8.        </StackLayout>  
  9.    </ContentView.Content>  

Then we are using the BindableProperty to the code behind.

  1. public partial class UnderLineLabel : ContentView  
  2.     {  
  3.         public UnderLineLabel ()  
  4.         {  
  5.             InitializeComponent ();  
  6.         }  
  7.         public string Text  
  8.         {  
  9.             get { return (string)GetValue(TextProperty); }  
  10.             set { SetValue(TextProperty, value); }  
  11.         }  
  12.         public static BindableProperty TextProperty = BindableProperty.Create(nameof(Text),  
  13.                                                   typeof(string),  
  14.                                                   typeof(UnderLineLabel),  
  15.                                                   defaultBindingMode: BindingMode.TwoWay,  
  16.                                                   propertyChanged: (bindable, oldVal, newVal) =>  
  17.                                                   {  
  18.                                                       var matEntry = (UnderLineLabel)bindable;  
  19.                                                       matEntry.customLabel.Text = (string)newVal;  
  20.                                                   });  
  21.         public Color LineColor  
  22.         {  
  23.             get { return (Color)GetValue(LineColorProperty); }  
  24.             set { SetValue(LineColorProperty, value); }  
  25.         }  
  26.         public static BindableProperty LineColorProperty = BindableProperty.Create(nameof(LineColor),  
  27.                                                   typeof(Color),  
  28.                                                   typeof(UnderLineLabel),  
  29.                                                    Color.Default,  
  30.                                                   defaultBindingMode: BindingMode.TwoWay,  
  31.                                                   propertyChanged: (bindable, oldVal, newVal) =>  
  32.                                                   {  
  33.                                                       var matEntry = (UnderLineLabel)bindable;  
  34.                                                       matEntry.customBox.BackgroundColor= (Color)newVal;  
  35.                                                   });  
  36.   
  37.         public static readonly BindableProperty TextColorProperty =  
  38.           BindableProperty.Create(  
  39.               nameof(TextColor),  
  40.               typeof(Color),  
  41.               typeof(UnderLineLabel),  
  42.               Color.Default,  
  43.               defaultBindingMode: BindingMode.TwoWay,  
  44.             propertyChanged: (bindable, oldVal, newVal) =>  
  45.             {  
  46.                 var matEntry = (UnderLineLabel)bindable;  
  47.                 matEntry.customLabel.TextColor= (Color)newVal;  
  48.             });  
  49.   
  50.         public Color TextColor  
  51.         {  
  52.             get { return (Color)GetValue(TextColorProperty); }  
  53.             set { SetValue(TextColorProperty, value); }  
  54.         }  
  55.     }  

Please make sure to add view reference.....

xmlns:local="clr-namespace:MyNewApp"

 <local:UnderLineLabel Text="Forget Password" TextColor="Brown" LineColor="Orange" HorizontalOptions="EndAndExpand"/>

TADAAA!  

You should have your Custom UnderLine working!!

Features of Custom UnderLine controls.

  1. Text Color Property= ( TextColor="BlueViolet")
  2. Text Property = (Text="Xamarin Skills")
  3. Line Color Property=( LineColor="Orange")
Ebook Download
View all
Learn
View all