Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I add a line feed into a Xamarin.Forms label?

Here is what I would like to do:

<Label Text="Choose By\nOne\nTwo" Margin="0,10,0,0" />

However the only thing this does is make the characters \n appear twice in the label. Is there any way that I can add a line feed in as part of a label's text?

like image 632
Alan2 Avatar asked Oct 21 '25 15:10

Alan2


2 Answers

You should use

<Label Text="Choose By &#10;One &#10;Two" Margin="0,10,0,0" />

and the result will look like \n

enter image description here

like image 165
GeralexGR Avatar answered Oct 24 '25 03:10

GeralexGR


I just ran into this, but for me I was sending a string from a server to the device and needed a way to keep line feeds in the text. I ended up having to process the text on the device and find my special <THIS IS A LINE FEED> character and replace it with Environment.NewLine.

So something like this:

In your XAML:

<Label Text="{Binding Something}" Margin="0,10,0,0" />

OR

<Label x:Name="LabelMy" Margin="0,10,0,0" />

Then in code behind or ViewModel:

string normalText = "Choose By\nOne\nTwo";

string fixedText = normalText.Replace("\n", Environment.NewLine);

ViewModel.Something = fixedText;

//OR

LabelMy.Text = fixedText;
like image 37
hvaughan3 Avatar answered Oct 24 '25 03:10

hvaughan3