字体
在Xamarin.Forms中设置字体
本文介绍了Xamarin.Forms如何在显示文本的控件上指定字体属性(包括重量和大小)。字体信息可以在代码中指定或 在Xaml中指定。也可以使用自定义字体。
在代码中设置字体
使用显示文本的任何控件的三个字体相关的属性:
- FontFamily -
string
字体名称。 - FontSize - 字体大小为a
double
。 - FontAttributes - 指定样式信息(如斜体和粗体)(使用
FontAttributes
C#中的枚举)的字符串。
此代码显示如何创建标签并指定要显示的字体大小和权重:
var about = new Label {
FontSize = Device.GetNamedSize (NamedSize.Medium, typeof(Label)),
FontAttributes = FontAttributes.Bold,
Text = "Medium Bold Font"
};
字体大小
该FontSize
属性可以设置为双重值,例如:
label.FontSize = 24;
您还可以使用NamedSize
具有四个内置选项的枚举; Xamarin.Forms为每个平台选择最佳尺寸。
- 微
- 小
- 中
- 大
该NamedSize
枚举可用于任何一个FontSize
可以使用指定Device.GetNamedSize
方法的值转换为double
:
label.FontSize = Device.GetNamedSize(NamedSize.Small, typeof(Label));
字体属性
可以在属性上设置字体样式(如粗体和斜体)FontAttributes
。目前支持以下值:
- 没有
- 胆大
- 斜体
该FontAttribute
枚举可以使用如下(可以指定单个属性或OR
共同它们):
label.FontAttributes = FontAttributes.Bold | FontAttributes.Italic;
FormattedString
一些Xamarin.Forms控件(如Label
)也支持使用FormattedString
该类的字符串中不同的字体属性。A FormattedString
由一个或多个Span
s组成,每个都可以有自己的格式属性。
本Span
类具有以下属性:
- 文本 - 要显示的值
- FontFamily - 字体名称
- FontSize - 字体大小
- FontAttributes - 样式信息,如斜体和粗体
- 前景色 - 文字颜色
- BackgroundColor - 背景颜色
创建和显示a FormattedString
的示例如下所示 - 请注意,它被分配给标签的FormattedText
属性而不是Text
属性。
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;
设置每个平台的字体信息
或者,该Device.OnPlatform
方法可用于在每个平台上轻松设置不同的字体名称,如代码所示:
label.FontFamily = Device.OnPlatform (
iOS: "MarkerFelt-Thin"
Android: "Droid Sans Mono"
WinPhone: "Comic Sans MS"
);
label.FontSize = Device.OnPlatform (
24,
Device.GetNamedSize (NamedSize.Medium, label),
Device.GetNamedSize (NamedSize.Large, label)
);
iOS的一个很好的字体信息来源是iosfonts.com。
在Xaml中设置字体
Xamarin.Forms控件显示文本都具有Font
可以在Xaml中设置的属性。在Xaml中设置字体的最简单方法是使用命名的大小枚举值,如下例所示:
<Label Text="Login" FontSize="Large"/>
<Label Text="Instructions" FontSize="Small"/>
该Font
属性有一个内置的转换器,允许所有字体设置在Xaml中表示为一个字符串值。以下示例显示如何在Xaml中指定字体属性和大小:
<Label Text="Italics are supported" FontAttributes="Italic" />
<Label Text="Biggest NamedSize" FontSize="Large" />
<Label Text="Use size 72" FontSize="72" />
要指定多重Font
设置,请将所需设置合并到单个字体属性字符串中。字体属性字符串的格式应为"[font-face],[attributes],[size]"
。参数的顺序很重要,所有参数都是可选的,attributes
可以指定多个参数,例如:
<Label Text="Small bold text" FontAttributes="Bold" FontSize="Micro" />
<Label Text="Really big italic text" FontAttributes="Italic" FontSize="72" />
该FormattedString
班还可以在XAML中使用,如下所示:
<Label>
<Label.FormattedText>
<FormattedString>
<FormattedString.Spans>
<Span Text="Red, " ForegroundColor="Red" FontAttributes="Italic" FontSize="20" />
<Span Text=" blue, " ForegroundColor="Blue" FontSize="32" />
<Span Text=" and green! " ForegroundColor="Green" FontSize="12"/>
</FormattedString.Spans>
</FormattedString>
</Label.FormattedText>
</Label>
Device.OnPlatform
也可以在XAML中使用,以在每个平台上呈现不同的字体。以下示例使用iOS(MarkerFelt-Thin)上的自定义字体,并仅在其他平台上指定大小/属性:
<Label Text="Login">
<Label.FontFamily>
<OnPlatform x:TypeArguments="x:String">
<OnPlatform.iOS>MarkerFelt-Thin</OnPlatform.iOS>
<OnPlatform.Android></OnPlatform.Android>
<OnPlatform.WinPhone></OnPlatform.WinPhone>
</OnPlatform>
</Label.FontFamily>
</Label>
当指定自定义字体时,始终是一个好主意OnPlatform
,因为很难找到所有平台上可用的字体。
使用自定义字体
使用不同于内置字体的字体需要一些平台特定的编码。此屏幕截图显示了使用Xamarin.Forms 从Google,Android和Windows Phone上提供的Google开源字体的自定义字体Lobster。
每个平台所需的步骤如下。将自定义字体文件与应用程序相结合时,请确保验证字体许可证是否允许分发。
iOS版
首先确保它被加载,然后通过名称使用Xamarin.Forms Font
方法引用它可以显示自定义字体。按照此博客文章中的说明进行操作:
- 使用Build Action添加字体文件:BundleResource和
- 然后更新Info.plist文件(由应用程序提供的字体,或
UIAppFonts
键) - 在Xamarin.Forms中定义一个字体的地方,通过名称来指定!
new Label {
Text = "Hello, Forms!",
FontFamily = Device.OnPlatform (
"Lobster-Regular",
null,
null
), // set only for iOS
}
Android的
Xamarin.Forms for Android可以通过遵循特定的命名标准来引用已添加到项目中的自定义字体。首先将字体文件添加到应用程序项目中的Assets文件夹,然后设置Build Action:AndroidAsset。然后使用完整路径和字体名称以散列(#)分隔为Xamarin.Forms中的字体名称,如下面的代码片段所示:
new Label
{
Text = "Hello, Forms!",
FontFamily = Device.OnPlatform(
null,
"Lobster-Regular.ttf#Lobster-Regular", // Android
null
)
}
视窗
Xamarin.Forms for Windows平台可以通过遵循特定的命名标准来引用已添加到项目中的自定义字体。首先将字体文件添加到应用程序项目中的/ Assets / Fonts /文件夹中,然后设置Build Action:Content。然后使用完整路径和字体文件名,后跟哈希(#)和字体名称,如下面的代码片段所示:
new Label {
Text = "Hello, Forms!",
FontFamily = Device.OnPlatform (
null,
null,
"Assets/Fonts/Lobster-Regular.ttf#Lobster" // Windows platforms
)
}
请注意,字体文件名和字体名可能不同。要在Windows上发现字体名称,请右键单击.ttf文件,然后选择预览。然后可以从预览窗口确定字体名称。
该应用程序的通用代码现已完成。平台专用电话拨号器代码现在将作为DependencyService实现。
XAML
您还可以Device.OnPlatform
在XAML中使用自定义字体:
<Label Text="Hello Forms with XAML">
<Label.FontFamily>
<OnPlatform x:TypeArguments="x:String">
<OnPlatform.iOS>Lobster-Regular</OnPlatform.iOS>
<OnPlatform.Android>Lobster-Regular.ttf#Lobster-Regular</OnPlatform.Android>
<OnPlatform.WinPhone>Assets/Fonts/Lobster-Regular.ttf#Lobster</OnPlatform.WinPhone>
</OnPlatform>
</Label.FontFamily>
</Label>
概要
Xamarin.Forms提供简单的默认设置,让您可以轻松地为所有支持的平台大小文本。它还允许您指定字体的面部和大小 - 即使不同的平台,当需要更细粒度的控制。的FormattedString
类可用于构建含有使用不同的字体规格的字符串Span
类。
也可以在Xaml中使用正确格式化的字体属性或FormattedString
带有Span
子级的元素来指定字体信息。