简体   繁体   中英

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?

You should use

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

and the result will look like \\n

在此处输入图片说明

Replace the \\n by &#x0a; . Or set it in a more verbose mode like:

<Label Margin="0,10,0,0">
    <Label.Text>
        Choose By
        One
        Two
    </Label.Text>
</Label>

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM