Xamarin.Forms:自定义字体和FontAttributes

时间:2018-08-06 13:53:08

标签: android ios fonts xamarin.forms attributes

我从事的是 Xamarin.Forms项目,在其中我添加了2种 Google字体MontserratRobotoCondensed

我可以在iOS和Android上的带有标签或按钮的应用程序中毫无问题地使用这些字体。

但是当我尝试应用FontAttributesItalic之类的Bold时,这是行不通的。

我首先只添加了“主要”字体(“蒙特塞拉特-常规”和“ RobotoCondensed-常规”)。然后,我添加了“辅助”字体(“ Italic”,“ Bold”,“ BoldItalic”),但这并没有任何改变。

我在做错什么吗?

2 个答案:

答案 0 :(得分:0)

  • 您还需要包括RobotoCondensed-Bold字体以应用粗体效果,仅使用RobotoCondensed字体就不能应用该效果。
  • 对于自定义字体,您需要添加这些字体以获得效果。
  • 如果您想应用boldItalic字体,请使用RobotoCondensed-BoldRobotoCondensed-Italic之类的字体。

您可以通过以下链接引用此链接:https://xamarinhelp.com/custom-fonts-xamarin-forms/

希望这可以解决您的问题。

答案 1 :(得分:0)

in this blog post和提到的in the docs所述,您需要添加所需的所有字体样式。例如RobotoCondensed-boldRobotoCondensed-regularRobotoCondensed-italic

然后,您可以通过特定于平台的方式来引用它们。在android上,您放置在Assets/Fonts中的字体由字符串Fonts/RobotoCondensed-bold.ttf#RobotoCondensed-bold引用,其中第一部分由文件名确定,而#之后的部分为{{3 }}。 在iOS上,您只需要字体名称。

  

FontFamily = Device.RuntimePlatform == Device.iOS ? "Lobster-Regular" : null // set only for iOS

     

FontFamily = Device.RuntimePlatform == Device.Android ? "Lobster-Regular.ttf#Lobster-Regular" : null // set only for Android

要简化字体的引用,可以在App.xaml中定义特定于平台的样式:

<OnPlatform x:TypeArguments="x:String" x:Key="MyFontRegular">
   <On Platform="Android" Value="Fonts/myfont-regular.ttf#MyFont-Regular" />
   <On Platform="iOS" Value="MyFont-Regular" />
</OnPlatform>

设置FontFamily="{StaticResource MyFontRegular}"以应用此字体。这样可以有效停用在同一元素上设置的FontAttributes的效果。

如果您的字体样式只是常见的regularbolditalic,则您可以 通过设置{ {1}},在其中可以找到字体家族名称的方式与找到字体的PostscriptName的方式类似。不过,我偶然发现了使用FontAttributes的选项。我不知道这是否被记录为行为。而且它似乎不适用于FontFamily="My FontFamily Name"FontAttributes之类的其他样式。还需要指出的是,设置FontFamily和FontAttributes的结果与指定包含样式的确切字体并不完全一样。使用dashed可能会使Xamarin忽略medium

相关问题