15
votes

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?

6
It should automatically split into two lines. - Utsav Dawn
Of course it should, but unfortunately it doesn't. Please note that the text is received from a resource file, it is not part of the XAML. - IdoT
Does it happen on iOS/Android? - Prashant Cholachagudda

6 Answers

2
votes

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.

39
votes

Very simple add following unicode instead of \n

 &#10;

for ex:

Text="General &#10; Studies - I"
4
votes

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

2
votes

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

2
votes

You can do that it like that:

Text = "First line string&#x0a;Second line string"
0
votes

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