How to add new line in FormattedString

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.


You can use Environment.NewLine to move text to the next line like this:

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

Tags:

Xamarin