Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 722
Neelam Prajapati Avatar asked Dec 02 '22 12:12

Neelam Prajapati


1 Answers

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/

like image 135
Sven-Michael Stübe Avatar answered Dec 18 '22 02:12

Sven-Michael Stübe