我从事的是 Xamarin.Forms项目,在其中我添加了2种 Google字体:Montserrat和RobotoCondensed。
我可以在iOS和Android上的带有标签或按钮的应用程序中毫无问题地使用这些字体。
但是当我尝试应用FontAttributes
或Italic
之类的Bold
时,这是行不通的。
我首先只添加了“主要”字体(“蒙特塞拉特-常规”和“ RobotoCondensed-常规”)。然后,我添加了“辅助”字体(“ Italic”,“ Bold”,“ BoldItalic”),但这并没有任何改变。
我在做错什么吗?
答案 0 :(得分:0)
RobotoCondensed-Bold
字体以应用粗体效果,仅使用RobotoCondensed
字体就不能应用该效果。bold
或Italic
字体,请使用RobotoCondensed-Bold
或RobotoCondensed-Italic
之类的字体。您可以通过以下链接引用此链接:https://xamarinhelp.com/custom-fonts-xamarin-forms/
希望这可以解决您的问题。
答案 1 :(得分:0)
如in this blog post和提到的in the docs所述,您需要添加所需的所有字体样式。例如RobotoCondensed-bold
,RobotoCondensed-regular
和RobotoCondensed-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
的效果。
如果您的字体样式只是常见的regular
,bold
和italic
,则您可以 通过设置{ {1}},在其中可以找到字体家族名称的方式与找到字体的PostscriptName的方式类似。不过,我偶然发现了使用FontAttributes
的选项。我不知道这是否被记录为行为。而且它似乎不适用于FontFamily="My FontFamily Name"
或FontAttributes
之类的其他样式。还需要指出的是,设置FontFamily和FontAttributes的结果与指定包含样式的确切字体并不完全一样。使用dashed
可能会使Xamarin忽略medium
。