简体   繁体   中英

justify text in label xamarin forms

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

在此输入图像描述
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 }

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

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM