Forum Xamarin.Forms
We are excited to announce that the Xamarin Forums are moving to the new Microsoft Q&A experience. Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

We encourage you to head over to Microsoft Q&A for .NET for posting new questions and get involved today.

How to use custom font in shared library

I have several xamarin forms applications using a single .net standard library with many shared components to maintain look and feel.

How do I go about setting up a custom font on these controls so it is configured in one central location. I have read through several articles like which all mention editing the plist file and copying files in the specific android/ios assets directories.

Is there a way to do this in a centralised library? How would a third party component library go about doing this?

I have so far added my two fonts (open sans) to /Assets/Fonts folder in my shared lib, set the build action to "content" and copy to output directory to "copy if newer" I see them appear in my bin folder.

I have a resource dictionary referencing these:

<ResourceDictionary
    xmlns="http://xamarin.com/schemas/2014/forms"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    x:Class="AlloyMobileComponents.Resources.FontResources">
  <!-- see /Assets/Fonts for font files -->
  <OnPlatform
      x:TypeArguments="x:String"
      x:Key="NormalFont">
    <On
        Platform="Android"
        Value="Assets/Fonts/OpenSans-Regular.ttf#Open Sans" />
    <On
        Platform="iOS"
        Value="OpenSans-Regular" />
  </OnPlatform>
  <OnPlatform
      x:TypeArguments="x:String"
      x:Key="BoldFont">
    <On
        Platform="Android"
        Value="Assets/Fonts/OpenSans-Bold.ttf#Open Sans" />
    <On
        Platform="iOS"
        Value="OpenSans-Bold" />
  </OnPlatform>
</ResourceDictionary>

And I consume this in my components using {DynamicResource BoldFont} etc.

Solution cleaned, rebuilt and run and I get:

**Java.Lang.RuntimeException:** 'Font asset not found Assets/Fonts/OpenSans-Bold.ttf'

any ideas?

Answers

  • Adeel984Adeel984 Member ✭✭✭

    Use it like this also Your fonts .ttf files should be in Assets folder and double check the build action should be Android Asset

    <ResourceDictionary xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="AlloyMobileComponents.Resources.FontResources"> <!-- see /Assets/Fonts for font files --> <OnPlatform x:TypeArguments="x:String" x:Key="NormalFont"> <On Platform="Android" Value="OpenSans-Regular.ttf#Open Sans" /> <On Platform="iOS" Value="OpenSans-Regular" /> </OnPlatform> <OnPlatform x:TypeArguments="x:String" x:Key="BoldFont"> <On Platform="Android" Value="OpenSans-Bold.ttf#Open Sans" /> <On Platform="iOS" Value="OpenSans-Bold" /> </OnPlatform> </ResourceDictionary>

  • cmcnicholascmcnicholas Member ✭✭

    @Adeel984 said:
    Use it like this also Your fonts .ttf files should be in Assets folder and double check the build action should be Android Asset

    <ResourceDictionary xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="AlloyMobileComponents.Resources.FontResources"> <!-- see /Assets/Fonts for font files --> <OnPlatform x:TypeArguments="x:String" x:Key="NormalFont"> <On Platform="Android" Value="OpenSans-Regular.ttf#Open Sans" /> <On Platform="iOS" Value="OpenSans-Regular" /> </OnPlatform> <OnPlatform x:TypeArguments="x:String" x:Key="BoldFont"> <On Platform="Android" Value="OpenSans-Bold.ttf#Open Sans" /> <On Platform="iOS" Value="OpenSans-Bold" /> </OnPlatform> </ResourceDictionary>

    if they are marked as android asset how will they work on ios?

  • Adeel984Adeel984 Member ✭✭✭

    You have to separately add the same files to iOS Resources folder and the build action for iOS will be Bundle resource then it'll work for iOS for android i have told you before.

  • cmcnicholascmcnicholas Member ✭✭

    Yer my mistake here was assuming I could do it all in one central .net standard library, I have added respective android and ios library projects and will try again.

  • jezhjezh Member, Xamarin Team Xamurai

    Yes, we have to add the font separately to the specific android/ios assets directories.

    At least for now, we cannot use custom font in android and ios platform from a common place

    For more about Fonts in Xamarin.Forms, you can check: https://docs.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/text/fonts

Sign In or Register to comment.