Add the Content View in your Project.
CustomLabel.xaml<?xml version="1.0" encoding="UTF-8"?> <ContentView xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:d="http://xamarin.com/schemas/2014/forms/design" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" x:Class="TestApp.CustomControl.CustomLabel"> <ContentView.Content> <StackLayout HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand"> <Label x:Name="customLabel" > </Label> <Label x:Name="lblReadMore" FontSize="18" FontAttributes="Bold" > <Label.GestureRecognizers> <TapGestureRecognizer Tapped="TapGestureRecognizer_Tapped" /> </Label.GestureRecognizers> </Label> </StackLayout> </ContentView.Content> </ContentView>
Create the bindable property and getter setter method for the label.
CustomLabel.xaml.cs
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Xamarin.Forms; using Xamarin.Forms.Xaml; namespace TestApp.CustomControl { [XamlCompilation(XamlCompilationOptions.Compile)] public partial class CustomLabel : ContentView { public CustomLabel() { InitializeComponent(); } #region Bindable Property public static readonly BindableProperty TextProperty = BindableProperty.Create( propertyName: nameof(TextProperty), returnType: typeof(string), declaringType: typeof(CustomLabel), defaultBindingMode: BindingMode.TwoWay, propertyChanged: TextPropertyChanged ); public string Text { get { return (string)base.GetValue(TextProperty); } set { base.SetValue(TextProperty, value); } } //Show the read more label if word length > 100 private static void TextPropertyChanged(BindableObject bindable, object oldValue, object newValue) { var control = (CustomLabel)bindable; if (newValue != null) { control.lblReadMore.IsVisible= false; control.customLabel.Text = (string)newValue; if (control.customLabel.Text.Split().Length>100) { control.ShortTextVisible = true; control.ReadMoreLabel = true; } } } #endregion public bool ReadMoreLabel { get; set; } private bool _shortTextVisible; public bool ShortTextVisible { get => _shortTextVisible; set { _shortTextVisible = value; ShortTextPropertyChanged(); } } //By Default show first 30 words. private void ShortTextPropertyChanged() { if (Text != null) { if (ShortTextVisible) { customLabel.Text = string.Join(" ",Text.Split().Take(30)); lblReadMore.Text = "Read More"; lblReadMore.IsVisible = true; } else { customLabel.Text = Text; lblReadMore.Text = "Read Less"; } } } private void TapGestureRecognizer_Tapped(object sender, EventArgs e) { ShortTextVisible = !ShortTextVisible; } } }
Implementation of Custom Control
Add the CustomLabel custom control on your content page.
MainPage.xaml
<?xml version="1.0" encoding="utf-8" ?> <ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:d="http://xamarin.com/schemas/2014/forms/design" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:TestApp.CustomControl" mc:Ignorable="d" x:Class="TestApp.MainPage"> <StackLayout Margin="10"> <!-- Place new controls here --> <local:CustomLabel Text="Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum." /> </StackLayout> </ContentPage>