Search code examples
xamarinlabelxamarin.formsunderline

How to underline single words in a label in Xamarin.Forms?


I would like to make underline text of label in Xamarin.Forms. I could not find any proper solution for this. One thing I found that there is no underline property for the text in a in Xamarin, so we could add a BoxView for the line instead like this:

<StackLayout  Grid.Row="0" Padding="0" VerticalOptions="Center">
    <Label Text="Terms and Conditions" />
    <BoxView BackgroundColor="White" HeightRequest="1" Margin="0,-8,0,0" />
</StackLayout>

But this is also not an appropriate solution as it will draw line to the whole StackView. I would like to underline only parts of text like the word "Terms" in above code.


Solution

  • You can use Effects for this.

    Create a UnderlineEffect using:

    Android

    var tv = (TextView)Control;
    tv.PaintFlags = tv.PaintFlags | PaintFlags.UnderlineText;
    

    iOS

    You have to use Mutable String as shown here: https://stackoverflow.com/a/19630376/1489968

    Edit

    I've written a blog post. Enjoy: http://smstuebe.de/2016/08/29/underlinedlabel.xamarin.forms/