How to add line breaks in a label in Xamarin Forms using XAML

I am trying to add line breaks to a label in Xamarin Forms using XAML, but it doesn't seem to work. This is the code I have right now that doesn't work:

<Label Text="All my text goes here etc\r\nawljfal alwef law fawlef lwemfalwfem" TextColor="White" WidthRequest="100" />

The '\r\n' actually outputs in the label text.

Posts

  • JoshuaNovakJoshuaNovak USMember ✭✭

    That worked perfectly. Thanks! :) Not sure why I didn't think about that lol. The only dumb thing is that if you tab the text over to be indented properly for your code the tabs show up in the label :(

  • powerdudepowerdude USMember ✭✭

    yeah, that was an annoying side-effect i noticed too. I think i've had better luck with Data-binding though.

  • AnsiAnsi INMember ✭✭

    I am using c# code instead of Xaml . How can I set Linebreak for label using xamarin.Forms ?

  • VincentPoirierVincentPoirier CAMember ✭✭

    @Ansi‌, try setting the Text property like this: "stringblabla\r\notherline"

  • voidstreamvoidstream FRMember ✭✭✭

    @VincentPoirier said:
    @Ansi‌, try setting the Text property like this: "stringblabla\r\notherline"

    The "\n" only works by code for me. How i can make a break line by XAML?

  • voidstreamvoidstream FRMember ✭✭✭
    edited December 2016

    @JulianPasque said:
    You can do it like this:
    Text="Stuff on line1&#x0a;Stuff on line 2"

    Works fine! Thanks :smiley:

  • JulianPasqueJulianPasque USMember ✭✭✭

    You are welcome :)

  • RanjithkumarRamakrishnanRanjithkumarRamakrishnan USMember ✭✭
    edited January 2017

    Add following unicode instead of \n

    &#10;

    Text="your first text &#10; your nextline text"

  • rzee7rzee7 INUniversity ✭✭✭✭✭

    @JulianPasque said:
    You can do it like this:
    Text="Stuff on line1&#x0a;Stuff on line 2"

    This has to be an answer here!

    Thanks, @JulianPasque :)

  • rzee7rzee7 INUniversity ✭✭✭✭✭

    Just in case if you someone getting string from server or making code behind then you should use \n instead of &#x0a; It doesn't work in dynamic string, only works in static string giving directly to label.

    Cheers!!
    { rzee }

  • VenkataSwamyVenkataSwamy INMember ✭✭✭
    edited May 2017

    Hii everyone,

    We can use LineBreakMode property for label to break the lines

  • VenkataSwamyVenkataSwamy INMember ✭✭✭

    Please find the Image

  • Zafar.4544Zafar.4544 USMember ✭✭
    edited May 2017

    \r\n has the special meaning when it is a C# string expression. In XAML '&lt' and '&gt' has meaning, but \r and \n are technically treated literal string chars. Since XAML is nothing but an XML file, the xaml file processor would consume them as literal strings.

  • IOSDevelopmentIOSDevelopment USMember

    @voidstream said:

    @JulianPasque said:
    You can do it like this:
    Text="Stuff on line1&#x0a;Stuff on line 2"

    Works fine! Thanks :smiley:
    Works fine! Thanks :smiley:

  • batmacibatmaci DEMember ✭✭✭✭✭

    @rzee7 said:
    Just in case if you someone getting string from server or making code behind then you should use \n instead of &#x0a; It doesn't work in dynamic string, only works in static string giving directly to label.

    Cheers!!
    { rzee }

    that is not working on binding

  • XavierRigauXavierRigau USMember ✭✭

    @voidstream said:

    @VincentPoirier said:
    @Ansi‌, try setting the Text property like this: "stringblabla\r\notherline"

    The "\n" only works by code for me. How i can make a break line by XAML?

    "\n" does even work from code. No mater if it is Label or Editor control.

  • hvaughanhvaughan USMember ✭✭✭
    edited October 2018

    If your trying to do it from code just use: Environment.NewLine

  • MNadeemMNadeem Member ✭✭

    @XavierRigau said:

    @voidstream said:

    @VincentPoirier said:
    @Ansi‌, try setting the Text property like this: "stringblabla\r\notherline"

    The "\n" only works by code for me. How i can make a break line by XAML?

    "\n" does even work from code. No mater if it is Label or Editor control.

    \n is working in my case in code

  • UnreachableCodeUnreachableCode USMember ✭✭✭

    How to do this from localized strings in AppResources? works but then formatting the resx file with ctrl+I turns it into what @powerdude suggested:

    <Label.Text>
    blah blah
    blah blah
    </Label.Text>

    This might be fine but I'm concerned that our Devops team here might remove newlines when they provide translations for our app.

    \n doesn't work and Environment.NewLine isn't an option in a resx file.

  • john_titushkojohn_titushko Member ✭✭
    edited August 24

    How about Data Binding? \r\n &#13; &#10; &#x0a; does not work....

    I found solution with Environment.NewLine .

    For example
    var text = "first-line, second-line, third-line";
    var text = Regex.Replace(text, @\,, Environment.NewLine);
    Listview.ItemsSource = text;

Sign In or Register to comment.