Xamrin.Forms 用户界面——控件——Text——Fonts

字体

在Xamarin.Forms中设置字体

PDF用于离线使用
示例代码:

让我们知道你对此的感受

最后更新:3个月前

本文介绍了Xamarin.Forms如何在显示文本的控件上指定字体属性(包括重量和大小)。字体信息可以在代码中指定或 在Xaml中指定。也可以使用自定义字体

在代码中设置字体

使用显示文本的任何控件的三个字体相关的属性:

  • FontFamily - string字体名称。
  • FontSize - 字体大小为a double
  • FontAttributes - 指定样式信息(如斜体粗体)(使用FontAttributesC#中的枚举)的字符串。

此代码显示如何创建标签并指定要显示的字体大小和权重:

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由一个或多个Spans组成,每个都可以有自己的格式属性。

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和Android上的自定义字体

每个平台所需的步骤如下。将自定义字体文件与应用程序相结合时,请确保验证字体许可证是否允许分发。

iOS版

首先确保它被加载,然后通过名称使用Xamarin.Forms Font方法引用它可以显示自定义字体。按照此博客文章中的说明进行操作:

  1. 使用Build Action添加字体文件:BundleResource
  2. 然后更新Info.plist文件(由应用程序提供的字体,或UIAppFonts键)
  3. 在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子级的元素来指定字体信息。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值