【问题标题】:justify text in label xamarin forms对齐标签 xamarin 表单中的文本
【发布时间】:2018-12-27 21:21:02
【问题描述】:

我试图在 Label Xamarin Forms 中对齐文本,但不能,
如何在不使用 webview 的情况下像图片一样对齐文本(不只是样式对齐)


我附上了我的代码,但它的理由只剩下了

        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 }

【问题讨论】:

  • 您的意思是设置文本样式吗?如果是,则需要手动制作。
  • 不只是造型,我使用 FormattedString()
  • "lblMSG.Horizo​​ntalTextAlignment = TextAlignment.Start;"使文本从左侧开始。尝试将其更改为 TextAlignment.Center。

标签: c# xamarin xamarin.forms


【解决方案1】:

而不是使用标签。我使用了 HtmlWebViewSource。

一个例子如下:

XAML:

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

XAML 背后的代码(在您的情况下为 viewModel.Model.Content = strText):

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

【讨论】:

  • 简单但有效。非常感谢。
  • 在您给定的解决方案中,我可以更改字体吗?我需要“montserrat”字体到我的 元素而不连接到 Internet 请给我任何解决方案或任何想法?
  • 您需要将字体文件添加到您的项目中,然后才能被引用。
【解决方案2】:

如果问题是关于将多行文本拉伸到全宽(文本必须接触块的左右两侧),您可以使用平台渲染器。 See this reply.

【讨论】:

    猜你喜欢
    • 2018-04-14
    • 2019-04-29
    • 1970-01-01
    • 2018-06-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多