【问题标题】:Decorated & Extended Text In a Label标签中的装饰和扩展文本
【发布时间】:2015-12-29 00:49:37
【问题描述】:

是否可以在 Xamarin.Forms 标签小部件中包含粗体和非粗体文本?是否可以在标签内也有结束线?

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

【问题讨论】:

    标签: c# xaml xamarin xamarin.forms


    【解决方案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>
    

    【讨论】:

    • 缺点是 Span.Text 不可绑定。
    • 你总是可以创建一个可绑定的继承!
    • @pmf 你不能总是创建一个可绑定的继承,因为有时类是密封的,这就是 跨度>
    • 当前实现中似乎存在一个小错误。当我们没有在每个 span 上指定 FontSize 时,字体大小继承自 Label,例如 NamedSize "Micro"。但是当我们在一个 span 中添加一个FontAttributes = FontAttributes.Bold 时,在 iOS 10 上,那个 span 的字体大小大约是其他 span 的两倍。可以通过设置 spans FontSize = Device.GetNamedSize(NamedSize.Micro, label) 来解决
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-20
    • 2012-07-31
    相关资源
    最近更新 更多