如何更改 Xamarin.Forms 中标签的字体系列? [英] How to change font family of label in Xamarin.Forms?
问题描述
我尝试使用 CSS 和 XAML 更改标签的字体系列,但字体没有反映.我正在尝试在我的应用程序中使用 Montserrat 字体.我该如何解决这个问题?
I tried to change the font-family of my label using CSS and XAML but the font is not reflecting. I am trying to use Montserrat font in my app. How can I fix this?
XAML 代码:
<Label StyleClass="label" Text="Sample"/>
CSS 代码:
.label{
font-family: Montserrat;
}
推荐答案
为了在您的项目中使用自定义字体,您必须执行以下操作:
In order to use custom fonts in your project, you have to do the following:
在您的 Android 项目中,将字体文件放在 Assets 文件夹中,并确保构建类型为 AndroidAsset.
In your Android Project, place your font file in your Assets folder, and ensure the build type is AndroidAsset.
然后您可以在您的 XAML 中声明资源字典中的字体(例如在 App.xaml 中
Then you can, in your XAML, declare the font in the resource dictionary (For example in 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>
要使用自定义字体,您只需:
To use the custom font, you can simply:
<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>
这篇关于如何更改 Xamarin.Forms 中标签的字体系列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!