Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to underline label with underline effect in Xamarin Forms?

I followed this tutorial to create underline effect. However, when my page starts it breaks without exception being caught. Has anyone managed to create underline effect? Here is a code:

UnderlineEffect.cs:

namespace XX.CustomForms
{
    public class UnderlineEffect : RoutingEffect
    {
        public const string EffectNamespace = "XX.CustomForms";

        public UnderlineEffect() : base($"{EffectNamespace}.{nameof(UnderlineEffect)}")
        {
        }
    }
}

UnderlineLabel_Droid.cs:

[assembly: ResolutionGroupName(UnderlineEffect.EffectNamespace)]
[assembly: ExportEffect(typeof(UnderlineEffect), nameof(UnderlineEffect))]
namespace XX.Droid.Renderers
{
    public class UnderlineEffect : PlatformEffect
    {
        protected override void OnAttached()
        {
            SetUnderline(true);
        }

        protected override void OnDetached()
        {
            SetUnderline(false);
        }

        protected override void OnElementPropertyChanged(System.ComponentModel.PropertyChangedEventArgs args)
        {
            base.OnElementPropertyChanged(args);

            if (args.PropertyName == Label.TextProperty.PropertyName || args.PropertyName == Label.FormattedTextProperty.PropertyName)
            {
                SetUnderline(true);
            }
        }

        private void SetUnderline(bool underlined)
        {
            try
            {
                var textView = (TextView)Control;
                if (underlined)
                {
                    textView.PaintFlags |= PaintFlags.UnderlineText;
                }
                else
                {
                    textView.PaintFlags &= ~PaintFlags.UnderlineText;
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Cannot underline Label. Error: ", ex.Message);
            }
        }
    }
}

And my xaml:

    xmlns:custom="clr-namespace:XX.CustomForms;assembly=XX"

    <Label Text="Privacy Notice" HorizontalTextAlignment="Center" >
        <Label.Effects>
            <custom:UnderlineEffect />
        </Label.Effects>
    </Label>
like image 517
Uros Avatar asked Mar 24 '17 09:03

Uros


3 Answers

Xamarin Forms added a TextDecorations property to Labels. Update to Xamarin Forms 3.3.0+ and just set:

C#

Label label = new Label {
    TextDecorations = TextDecorations.Underline
}

XAML

<Label TextDecorations="Underline"/>

Docs Link

Be aware that there was a bug on iOS when an underlined Label is in a ListView. Looks like it has been fixed and released in 3.5.0. I am still using a custom renderer on iOS for now until I am ready to update to the latest version.

GitHub issue

So continue using the iOS effect if you have not updated to XF 3.5.0 yet.

like image 68
hvaughan3 Avatar answered Oct 18 '22 10:10

hvaughan3


The lengths some people are going to to get underlined text in Xamarin is insane. Here's a way to do it without a thousand line custom renderer. The negative margin trick came from this guy.

<StackLayout HorizontalOptions="Start">
    <Label Text="Underlined Text" />
    <BoxView HeightRequest="1" BackgroundColor="Purple" HorizontalOptions="FillAndExpand" Margin="0,-7,0,0" />
</StackLayout>
like image 12
user875234 Avatar answered Oct 18 '22 09:10

user875234


Use TextDecorations property in Label class.

<Label Text="Underlined Text" TextDecorations="Underline"/>

like image 4
Bhauraj Biradar Avatar answered Oct 18 '22 09:10

Bhauraj Biradar