c# - 如何更改 Xamarin.Forms 中标签的字体系列?

标签 c# xamarin xamarin.forms xamarin.android

我尝试使用 CSS 和 XAML 更改标签的字体系列,但字体没有反射(reflect)出来。我正在尝试在我的应用程序中使用 Montserrat 字体。我该如何解决这个问题?

XAML 代码:

<Label StyleClass="label" Text="Sample"/>

CSS 代码:

.label{
    font-family: Montserrat;
}

最佳答案

为了在您的项目中使用自定义字体,您必须执行以下操作:

在您的 Android 项目中,将您的字体文件放在您的 Assets 文件夹中,并确保构建类型是 AndroidAsset。

然后您可以在您的 XAML 中声明资源字典中的字体(例如在 App.xaml 中

<ResourceDictionary>
    <OnPlatform x:TypeArguments="x:String" x:Key="BoldFont">
        <On Platform="Android" Value="OpenSans-Bold.ttf#Open Sans" />
        <On Platform="UWP" Value="/Assets/OpenSans-Bold.ttf#Open Sans" />
        <On Platform="iOS" Value="OpenSans-Bold" />
    </OnPlatform>
    <OnPlatform x:TypeArguments="x:String" x:Key="NormalFont">
        <On Platform="Android" Value="OpenSans-Regular.ttf#Open Sans" />
        <On Platform="UWP" Value="/Assets/OpenSans-Regular.ttf#Open Sans" />
        <On Platform="iOS" Value="OpenSans-Regular" />
    </OnPlatform>
</ResourceDictionary>

要使用自定义字体,您可以简单地:

<StackLayout>
    <Label Text="Welcome to Xamarin Forms! (OpenSans-Bold)" FontFamily="{StaticResource BoldFont}" />
    <Label Text="Welcome to Xamarin Forms! (OpenSans-Regular)" FontFamily="{StaticResource NormalFont}" />
    <Label Text="Welcome to Xamarin Forms! (Default)" />
</StackLayout>

关于c# - 如何更改 Xamarin.Forms 中标签的字体系列?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52689641/

相关文章:

c# - 从 int 获取颜色

c# - 什么时候应该调用 Page.Header.DataBind?

c# - 消息 AOT 问题 Attempting to JIT compile method with Pinvoke on Xamarin iOS

c# - 在 xamarin.forms 中使用 fontawesome(仅限 iOS 和 Android)

azure - 如何使用 IMobileServiceSyncTable - InsertAsync 捕获后端服务器的异常?

c# - 百万对象创建的性能建议

c# - Realm Xamarin - GetInstance() 错误

android - 找不到与给定名称 Xamarin Android 匹配的资源

xamarin.forms - 如何在 Xamarin.Form 中存储用户登录凭据

c# - 在 c# 的 MySQL 查询中使用文字下划线字符