首页 文章

标签中的装饰和扩展文本

提问于
浏览
3

是否可以在Xamarin.Forms Label小部件中使用粗体和非粗体文本?是否有可能在标签内部有终结点?

<Label Text="Abc <b>def</b>"/>
<Label Text="Abc \ndef"/>

2 回答

  • 1

    最好的方法是绑定FormattedText样式,如下所示:

    <Label FormattedText="{Binding CustomFormattedText}" />
    

    在模型中:

    public FormattedString CustomFormattedText
        {
            get
            {
                return new FormattedString
                {
                    Spans = {
                        new Span { Text = Sum, FontAttributes=FontAttributes.Italic, FontSize="10" },
                        new Span { Text = Info, FontSize="10" } }
                };
            }
            set { }
        }
    
  • 2

    看看the documentation中的 FormattedString .

    您创建一个 FormattedString 对象,该对象由多个 Span 对象组成,这些对象都可以拥有自己的字体,重量,颜色等 .

    例:

    var labelFormatted = new Label ();
    var fs = new FormattedString ();
    fs.Spans.Add (new Span { Text="Red, ", ForegroundColor = Color.Red, FontSize = 20, FontAttributes = FontAttributes.Italic) });
    fs.Spans.Add (new Span { Text=" blue, ", ForegroundColor = Color.Blue, FontSize = 32) });
    fs.Spans.Add (new Span { Text=" and green!", ForegroundColor = Color.Green, FontSize = 12) });
    labelFormatted.FormattedText = fs;
    

    关于新行, \n 应该有效 . 另一种解决方法是这样做:

    <Label>
    <Label.Text>
    This is my magical
    unicorn text
    </Label.Text>
    </Label>
    

相关问题