4

Am trying to justify text in Label Xamarin Forms but couldnt ,
how to justify the text (not styling justify only) same like a picture without using webview

enter image description here
I have attached my code but its justify left only

        string strText="MY LONG TEXT IS GOING HERE";

        var lblMSG = new Label {TextColor = Color.Black };

        lblMSG.Text=strText;
        lblMSG.LineBreakMode = LineBreakMode.WordWrap;
        lblMSG.HorizontalOptions = LayoutOptions.Fill;
        lblMSG.HorizontalTextAlignment = TextAlignment.Start;
        lblMSG.VerticalTextAlignment = TextAlignment.Center;
        StackLayout  stk=   new StackLayout { Children = { lblMSG}, BackgroundColor = Color.White ,HorizontalOptions =LayoutOptions.FillAndExpand }
Medo Medo
  • 952
  • 2
  • 12
  • 21

2 Answers2

4

Instead of using a label. I used a HtmlWebViewSource.

An example is below:

XAML:

 <StackLayout x:Name="webViewLayout"
    HorizontalOptions="FillAndExpand"
    VerticalOptions="FillAndExpand">
    <!-- view codebehind for WebView for overview text -->
 </StackLayout>

Code Behind of XAML (viewModel.Model.Content = strText in your case):

var browser = new WebView();
browser.HorizontalOptions = LayoutOptions.FillAndExpand;
browser.VerticalOptions = LayoutOptions.FillAndExpand;

var source = new HtmlWebViewSource();
var text = "<html>" +
        "<body  style=\"text-align: justify;\">" +
        String.Format("<p>{0}</p>", viewModel.Model.Content) +
        "</body>" +
        "</html>";
source.Html = text ;
browser.Source = source;
webViewLayout.Children.Add(browser);
nishantvodoo
  • 835
  • 3
  • 17
  • 32
1

If the question is about stretching the multi-line text to full width (text must touch left and right sides of the block), you can use platform renderer. See this reply.

Maxim Saplin
  • 4,115
  • 38
  • 29