Label and \n

FrancoisMFrancoisM ✭✭FRUniversity ✭✭

Hi,

For a Label, Text="A\nB" doesn' t display A on a line and B on a new line but "A\nB". How come?
the doc says to use \n for new lines so I'm lost.
Tks.

François

Posts

  • Shane000Shane000 ✭✭✭ USMember ✭✭✭
    edited December 2014

    http://forums.xamarin.com/discussion/24179/how-to-add-line-breaks-in-a-label-in-xamarin-forms-using-xaml
    Specifically the workaround is:
    <Label TextColor="White" WidthRequest="100"> <Label.Text> blah blah blah blah </Label.Text> </Label>

  • FrancoisMFrancoisM ✭✭ FRUniversity ✭✭

    I saw that post. Maybe I over-simplified my question. The text is bound to a view model where the value
    is computed so it can't be entered directly in xaml.

  • FredyWengerFredyWenger ✭✭✭✭✭ CHInsider ✭✭✭✭✭

    @FranoisMouchati‌ :

    Have you tried: "\r\n"..?
    I use this, if I want to create a new line in label (and in my app it works :-)...
    string cText = "whatever" + "\r\n" + "text for line 2"
    label.text = cText;
    Should display:
    whatever
    text for line 2

  • Shane000Shane000 ✭✭✭ USMember ✭✭✭

    You may also try using Environment.NewLine instead of manually typing \r\n

  • FrancoisMFrancoisM ✭✭ FRUniversity ✭✭

    Actually, the text is within a resource (for localization) so I can't use Environment.NewLine. And \r\n doesn't work either. However, shift+enter withtin the value of the string resource works.

  • FredyWengerFredyWenger ✭✭✭✭✭ CHInsider ✭✭✭✭✭

    If you use in code: :

    string cText = "whatever" + "\r\n" + "text for line 2"
    label.text = cText;

    that really don't work...?

  • FrancoisMFrancoisM ✭✭ FRUniversity ✭✭

    In a string resource it doesn't. In another situation if you say it works I'm sure it does

  • IdoTeneIdoTene ✭✭ USMember, University ✭✭

    I'm having the exact same problem, any solution for new line for text Lable (not from code)?

  • IdoTeneIdoTene ✭✭ USMember, University ✭✭

    @AlexLau - tried it, but it didn't work..

  • AlexLauAlexLau ✭✭ HKMember ✭✭

    @IdoT, could you paste your XAML here?

  • IdoTeneIdoTene ✭✭ USMember, University ✭✭

    Here it is:

    <Label FontSize="Medium" TextColor="Gray" Text="{translation:TranslationResource MyExpertsView_NoExpertsText}" IsVisible="{Binding IsNoExpertsFound}"></Label>

  • AlexLauAlexLau ✭✭ HKMember ✭✭

    @IdoT, if you are using Resource, then you cannot use &#10; because it is a XML entities and can used in XML only.

    In this case, you may simple input a line feed in the resources files. If you are using Visual Studio Resource Editor, press Shift + Enter to add the line feed.

  • IdoTeneIdoTene ✭✭ USMember, University ✭✭

    @AlexLau I can't add a line feed, since the text resources are stored in a remote DB

  • AlexLauAlexLau ✭✭ HKMember ✭✭

    @IdoT, then there is no way to do so in XAML because the Text property treats the input as text and will not allow any escape characters. You may consider pre-processing the strings (replacing all \n to line feeds) from your TranslationResource.

  • IdoTeneIdoTene ✭✭ USMember, University ✭✭

    thanks @AlexLau , I added this line to TranslationResource:
    value = value.Replace("\\n", Environment.NewLine);
    And now it parse correctly.

  • KruKru BEMember

    I used the combination (cr and lf) in my resource strings. This works on Android and iOS. No replacement functionality required.

  • KruKru BEMember

    Oops code not visible. I mean &#13;&#10;

  • AmiralKancaAmiralKanca USMember

    More than a year after your last message. But since there was no answer, I wanted to write. :smile:

    No, it will not work that way. Because the string comes from the database. So, you need to do that while the data is being loaded exactly into the Label. So you should use the Converter. I put my own example down there. Hopefully it benefits your business.

    public class NewLineConverter : IValueConverter {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture) {
            if (value != null) {
                value = (value as string).Replace("\\r\\n", Environment.NewLine);
                value = (value as string).Replace("\r\n", Environment.NewLine);
            }
            return value;
        }
    
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) {
            return null;
        }
    }
    

    And inside Label :
    Text="{Binding Model.NewLinedDetail, Converter={StaticResource NewLineConverter}}"

  • PhenmanPhenman ✭✭✭ USMember ✭✭✭

    \n, Environment.NewLine and LineBreakMode="WordWrap" not work on iOS. I use last version of XF: 3.3.0.912540. Also I had same problem with previous version.

Sign In or Register to comment.