Home » Android » android – Xamarin forms Label new line

android – Xamarin forms Label new line

Posted by: admin May 14, 2020 Leave a comment

Questions:

I have a Label in my Xamarin forms page, that receives it’s Text from it’s view model, and the text contains the \n character.
How can I make the Label to automatically split the text into two different rows?

How to&Answers:

Copied from a thread in Xamarin Forum

Probably your code:

<Label FontSize="Medium" TextColor="Gray" Text="{translation:TranslationResource MyExpertsView_NoExpertsText}"  IsVisible="{Binding IsNoExpertsFound}"></Label>

Since your text is retrieved from a resource file, the Text property of Label treats the input as plain text and will not allow any escape characters.

You may consider pre-processing the strings (replacing all \n to line feeds) from theTranslationResource.

Answer:

Very simple add following unicode instead of \n

 

for ex:

Text="General 
 Studies - I"

Answer:

One way to solve it would be replacing in your VM the ‘\n’ with System.Environment.NewLine

string Text = "Text first line\nText second line".Replace("\n", System.Environment.NewLine);

https://stackoverflow.com/a/56981145/4230687

Answer:

You can do that it like that:

Text = "First line string
Second line string"

Answer:

I just use \n, i’m using XAML and I’m binding the text through Binding on the label.

Answer:

The simplest way is use “Shift+Enter” to add new line in your resource