【发布时间】:2015-12-29 00:49:37
【问题描述】:
是否可以在 Xamarin.Forms 标签小部件中包含粗体和非粗体文本?是否可以在标签内也有结束线?
<Label Text="Abc <b>def</b>"/>
<Label Text="Abc \ndef"/>
【问题讨论】:
标签: c# xaml xamarin xamarin.forms
是否可以在 Xamarin.Forms 标签小部件中包含粗体和非粗体文本?是否可以在标签内也有结束线?
<Label Text="Abc <b>def</b>"/>
<Label Text="Abc \ndef"/>
【问题讨论】:
标签: c# xaml xamarin xamarin.forms
最好的方法是像这样绑定 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 { }
}
【讨论】:
在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>
【讨论】:
FontSize 时,字体大小继承自 Label,例如 NamedSize "Micro"。但是当我们在一个 span 中添加一个FontAttributes = FontAttributes.Bold 时,在 iOS 10 上,那个 span 的字体大小大约是其他 span 的两倍。可以通过设置 spans FontSize = Device.GetNamedSize(NamedSize.Micro, label) 来解决