3

Has anyone figured out how to center (or justify or in any way horizontally effect) text in a Xamarin Forms Label with LineBreakMode=WordWrap? Without using a WebView (or similar)?

All HorizontalOptions entries are cheerfully ignored in this code snippet:

<StackLayout>
    <Label
        Margin="75,0,75,0"
        FontAttributes="Italic"
        FontSize="Large"
        HorizontalOptions="Center"
        LineBreakMode="WordWrap"
        Text="Live life as though nobody is watching, and express yourself as though everyone Is listening."
        VerticalOptions="CenterAndExpand" />
</StackLayout>

It looks like this.

Omortis
  • 1,334
  • 19
  • 44

2 Answers2

5

You should use HorizontalTextAlignment="Center" in this case.

<StackLayout>
    <Label
        Margin="75,0,75,0"
        FontAttributes="Italic"
        FontSize="Large"
        HorizontalOptions="Center"
        HorizontalTextAlignment="Center"
        LineBreakMode="WordWrap"
        Text="Live life as though nobody is watching, and express yourself as though everyone Is listening."
        VerticalOptions="CenterAndExpand" />
</StackLayout>
Adlorem
  • 1,457
  • 1
  • 11
  • 10
  • Neat. I guess that makes sense, as `HorizontalOptions` only pertains to the location of the `Label` within the parent, right? Thanks! – Omortis Feb 11 '20 at 21:06
0

add this line in your code

HorizontalTextAlignment="Center"

<StackLayout>
    <Label
        Margin="75,0,75,0"
        FontAttributes="Italic"
        FontSize="Large"
        HorizontalOptions="Center"
        HorizontalTextAlignment="Center"
        LineBreakMode="WordWrap"
        Text="Live life as though nobody is watching, and express yourself as though everyone Is listening."
        VerticalOptions="CenterAndExpand" />
</StackLayout>
Chetan Rawat
  • 578
  • 3
  • 17