locked
How can I center justify Label text in a StackLayout? RRS feed

  • Question

  • User90084 posted

    I'm trying to get some label text to be center justified. Maybe I'm just a little tired, but can't really think through this, atm. Any suggestions on getting text to center justify?

    code <?xml version="1.0" encoding="UTF-8"?> <ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:local="clr-namespace:PROJ.Controls;assembly=PROJ" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="PROJ.Pages.Users.ForgotPasswordPage" BackgroundColor="White"> <ContentPage.Content> <StackLayout Padding="12, 0, 12, 0" Spacing="0"> <!-- Start: Need to center justify here --> <StackLayout Padding="0, 100, 0, 50" Spacing="0" Orientation="Vertical" HorizontalOptions="Center"> <StackLayout HorizontalOptions="Center"> <Label Text="Trouble logging in?" TextColor="#1e3c66" FontAttributes="Bold" /> <Label FontSize="Small" Text="No worries, you can reset your password by entering your email address below and clicking “reset password.”" TextColor="#81838c" LineBreakMode="WordWrap" /> </StackLayout> </StackLayout> <!-- End: Need to center justify here --> <local:ExtendedEntry x:Name="emailEntry" Placeholder="Email address" BackgroundColor="#00b6cb" HeightRequest="39" Keyboard="Email" TextColor="White" HorizontalOptions="FillAndExpand" VerticalOptions="Start" /> <BoxView Color="#03a3b5" HeightRequest="1" /> <Button x:Name="resetPasswordButton" Clicked="OnResetPasswordButtonClicked" Text="RESET PASSWORD" TextColor="White" BackgroundColor="#002a5c" FontAttributes="Bold" BorderRadius="0"
    BorderWidth="0" HeightRequest="45.5" HorizontalOptions="FillAndExpand" VerticalOptions="Start" /> <StackLayout Padding="0, 0, 0, 10" Spacing="40" Orientation="Horizontal" HorizontalOptions="FillAndExpand" VerticalOptions="EndAndExpand"> <StackLayout HorizontalOptions="StartAndExpand"> <Label x:Name="loginLabelButton" FontSize="Micro" Text="Login to my account" TextColor="#adb0ba"> <Label.GestureRecognizers> <TapGestureRecognizer Tapped="OnLoginLabelButtonClicked"/> </Label.GestureRecognizers> </Label> </StackLayout> <StackLayout HorizontalOptions="EndAndExpand"> <Label x:Name="signUpLabelButton" FontSize="Micro" Text="Sign up here!" TextColor="#adb0ba"> <Label.GestureRecognizers> <TapGestureRecognizer Tapped="OnSignUpLabelButtonClicked"/> </Label.GestureRecognizers> </Label> </StackLayout> </StackLayout> </StackLayout> </ContentPage.Content> </ContentPage>

    Friday, June 5, 2015 8:01 PM

Answers

  • User19820 posted

    Here it is:

    <!-- Start: Need to center justify here --> <StackLayout HorizontalOptions="FillAndExpand" Padding="0, 100, 0, 50" Spacing="0"> <Label Text="Trouble logging in?" TextColor="#1e3c66" FontAttributes="Bold" XAlign="Center"/> <Label FontSize="Small" Text="No worries, you can reset your password by entering your email address below and clicking “reset password.”" TextColor="#81838c" LineBreakMode="WordWrap" XAlign="Center" /> </StackLayout> <!-- End: Need to center justify here -->

    I hope this answers your question

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Friday, June 5, 2015 8:18 PM

All replies

  • User119704 posted

    Hello @Tekton,

    Contents inside a StackLayout are intended to flow left to right. You could try another layout container such a Grid instead.

    Let me know

    Friday, June 5, 2015 8:10 PM
  • User90084 posted

    Will check that out, thanks! :)

    Friday, June 5, 2015 8:11 PM
  • User19820 posted

    Here it is:

    <!-- Start: Need to center justify here --> <StackLayout HorizontalOptions="FillAndExpand" Padding="0, 100, 0, 50" Spacing="0"> <Label Text="Trouble logging in?" TextColor="#1e3c66" FontAttributes="Bold" XAlign="Center"/> <Label FontSize="Small" Text="No worries, you can reset your password by entering your email address below and clicking “reset password.”" TextColor="#81838c" LineBreakMode="WordWrap" XAlign="Center" /> </StackLayout> <!-- End: Need to center justify here -->

    I hope this answers your question

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Friday, June 5, 2015 8:18 PM
  • User90084 posted

    @stvansolano @AndreiNitescu XAlign works great! Danke schön! :sunglasses:

    Friday, June 5, 2015 8:22 PM
  • User19820 posted

    @Tekton Bitte schön

    Friday, June 5, 2015 8:38 PM
  • User188655 posted

    XAlign is obsolete. Please use HorizontalTextAlignment instead.

    Sunday, May 22, 2016 9:02 AM