3
votes

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 }
2
Do you mean styling the text? If yes, you need to make it manually.Danil Kurkin
not styling only justifying - for styling I use FormattedString()Medo Medo
"lblMSG.HorizontalTextAlignment = TextAlignment.Start;" makes the text start from the left. Try changing it to TextAlignment.Center.Richard Pike

2 Answers

3
votes

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);
0
votes

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.