I just wanted to ask if there is any way to justify text in a Label. I am using Xamarin Forms Xaml.
Thanks.
UPDATE: As for now, it is not possible to justify text. Most of the answers were about centering the text, but it is not what I asked. One way could be to use Renderer as by Timothy.
Try this:
The current way to do this is by using
HorizontalTextAlignment
and the values for theTextAlignment
enumeration are:Center
= Center-aligned textStart
= Left-alignedEnd
= Right-alignedCenter a label and its text example:
<Label x:Name="Description" HorizontalTextAlignment="Center" VerticalOptions="Center" HorizontalOptions="Center" />
What sort of container are you using to hold the text? Having a StackLayout with HorizontalOptions of FillAndExpand, along with the XAlign, might do it, but only if your text is only one line long per control.
Though you can't stretch label's text to a full width using Xamarin.Forms features, it's easily achieved with a platform renderer.
Most Xamarin platforms have the text justification feature available in corresponding native elements, and it's just a matter of setting a single attribute of a native element. I suppose the reason for not adding this feature to standard Xamarin.Forms label is lagging of platforms in that capability, e.g. Android had Android.Text.JustificationMode.InterWord flag added only in 8.1
Below you can see Android renderer implementation for Android:
In my example I used ExtenedLabel subclass of Xamarin.Forms.Label with extra property JustifyText to let setting the justification of the text. That's how the subclassed control can be declared:
If you use the Label under relativeLayout you can justify the label..
The trick is you must fill the width & height as per parent..
So I use HeightConstraint,WidthConstraint with factor=1.. so it take full width & height of the parent..
Use the
XAlign
property