Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add new line in FormattedString

Tags:

xamarin

 public FormattedString FormattedDescription
 {
     get
     {
         return new FormattedString
         {
            Spans = 
            {
                new Span 
                { 
                    Text = RoleName, 
                    FontSize = 16, 
                    FontAttributes = FontAttributes.Bold 
                },
                new Span 
                { 
                    Text = "/ " + ProjectRoleID + "/ "+Part + "/ "+Gender + "/ " + AgeRange
                },
           }
        };
    }
    set 
    { 
    }  
}

In above code i want to Display RoleName in First line and other Detail in second Line.

like image 353
jitendra rathore Avatar asked Apr 12 '16 08:04

jitendra rathore


People also ask

How do you insert a line break in typescript?

To add a new line in string in typescript, use new line \n with the + operator it will concatenate the string and return a new string with a new line separator. put the "\n" where you want to break the line and concatenate another string.

How do I add a new line in xamarin?

I just use \n , i'm using XAML and I'm binding the text through Binding on the label. Show activity on this post. To changed it on ViewModel, you can use this method. It'll update all \n on your string as string.


1 Answers

Here is a "Cleaner" Solution fully in XAML:

  • First, add this namespace to your XAML: xmlns:system="clr-namespace:System;assembly=netstandard"

This is the system namespace and it contain all system classes.

  • Second, call the Environment.NewLine in XAML, in the Span you wish as shown bellow:
<Label>
    <Label.FormattedText>
        <FormattedString>
            <Span Text="First Text"/>
            <Span Text=" "/>
            <Span Text="Second Text"/>
            <Span Text="{x:Static system:Environment.NewLine}"/>
            <Span Text="Above is a new line"/>
        </FormattedString>
    </Label.FormattedText>
</Label>

Here you have a clean way to show new lines.

like image 127
Damien Doumer Avatar answered Nov 02 '22 01:11

Damien Doumer