Xamarin Forms Label - Rationale?

I just wanted to ask if there is a way to justify the text in the label... I am using Xamarin Forms Xaml.

Thank.

UPDATE: It is not possible to justify the text at this time . Most of the answers were about centering the text, but that's not what I asked. One way is to use Renderer like Timothy.

+3


source to share


5 answers


The current way to do it is to use HorizontalTextAlignment

, and values ​​to enumerate TextAlignment

:

  • Center

    = center-aligned text
  • Start

    = Align left
  • End

    = Right align


Center the label and its text :

<Label x:Name="Description" HorizontalTextAlignment="Center" VerticalOptions="Center" HorizontalOptions="Center" />

+5


source


Use property XAlign



Label lbl = new Label();
lbl.Text = "I'm a Label!";
lbl.XAlign = TextAligntment.Start; // Start, Center, End are valid

      

+1


source


What container are you using to store your text? Having a StackLayout with HorizontalOptions from a FillAndExpand along with XAlign can do this, but only if your text is only one line long for each control.

0


source


Try the following:

<StackLayout HorizontalOptions="FillAndExpand" Padding="0, 10, 0, 10" Spacing="0">
      <Label Text="Test message" XAlign="Center"/>
      <Label FontSize="Small" Text="Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat." LineBreakMode="WordWrap"
XAlign="Center" />
</StackLayout>

      

0


source


If you use Label under relativeLayout you can justify the label.

The trick is that you have to fill the width and height according to the parent.

So I am using HeightConstraint, WidthConstraint with factor = 1 .. so it takes all the width and height of the parent.

  <RelativeLayout >

    <Label Text="My text"
           FontSize="20" 
           HorizontalOptions="Center" 
           VerticalOptions="Center"
           RelativeLayout.HeightConstraint="{ConstraintExpression Type=RelativeToParent,Property=Height,Factor=1,Constant=0}"
           RelativeLayout.WidthConstraint="{ConstraintExpression Type=RelativeToParent,Property=Width,Factor=1,Constant=0}" />
  </RelativeLayout>

      

0


source







All Articles