2

I work on a Xamarin.Forms project where I added 2 Google fonts: Montserrat and RobotoCondensed.

I can use these fonts without any problem in my app, with Labels or Buttons, on iOS and Android.

But when I try to apply a FontAttributes like Italic or Bold, this doesn't work.

I've first only added the "main" font ("Montserrat-Regular" and "RobotoCondensed-Regular"). Then I've added "secondary" fonts ("Italic", "Bold", "BoldItalic") but this doesn't change anything.

Is there something I'm doing wrong?

Gold.strike
  • 1,269
  • 13
  • 47

2 Answers2

1
  • You need to include RobotoCondensed-Bold font as well to apply bold effect, by only having RobotoCondensed font you can't apply that effect.
  • For custom fonts you need to add those fonts to achieve effects.
  • Where ever you want to apply bold or Italic fonts, use those fonts like RobotoCondensed-Bold or RobotoCondensed-Italic.

You can refer this link for the same: https://xamarinhelp.com/custom-fonts-xamarin-forms/

Hope this may solve your issue.

MShah
  • 1,247
  • 9
  • 14
1

As explained in this blog post and mentioned in the docs, you need to add all the font styles you want. e.g. RobotoCondensed-bold, RobotoCondensed-regular and RobotoCondensed-italic.

Then you can reference them in a platform-specific way. On android, a font that you placed in Assets/Fonts is referred to by the string Fonts/RobotoCondensed-bold.ttf#RobotoCondensed-bold where the first part is determined by the filename, and the part after the # is the PostscriptName of the font. On iOS, you only need the font name.

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

To make referring to fonts easier, you can define a platform-specific style in 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>

Set FontFamily="{StaticResource MyFontRegular}" to apply this font. This effectively deactivates the effects of FontAttributes set on the same element.

If your font styles are simply the common regular, bold and italic, you can refer to them using FontAttributes by setting FontFamily="My FontFamily Name", where the font family name can be found similarly to how you find the PostscriptName of the font. This option of using the FontAttributes is something I just stumbled upon, though. I do not know whether this is documented behaviour. And it does not seem to work for other styles like dashed or medium. It is also worth pointing out that the result from setting the FontFamily and FontAttributes does not look exactly the same like specifying the exact font with style included. It is possible, that using FontAttributes makes Xamarin ignore FontFamily.

lucidbrot
  • 5,378
  • 3
  • 39
  • 68
  • As of Xamarin.Forms 4.5.530 and up, there is no need to specify platform, nor add the font files to each of the native projects. Here is a great guide devblogs.microsoft.com/xamarin/embedded-fonts-xamarin-forms – chri3g91 Jun 03 '20 at 19:17