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.
source to share
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" />
source to share
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>
source to share
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>
source to share