Custom Fonts In Xamarin.Forms

Here we will see the steps to add custom fonts in Aamarin forms applications. Sometimes we have a requirement of showing our app with custom fonts or client selected fonts instead of the default fonts available in Xamarin forms.
 
If you want to add any custom font to your application, first we need to download our required font in to our system. If you unzip the downloaded font, you can find one '.ttf' file. For showing custom fonts, we need to add these '.ttf' files to each project. 
 
Android
 
For Android projects, add '.ttf' file in 'Assets' folder and set build type is "AndroidAsset"
 
Ios
 
For iOS projects, Add  '.ttf' file in 'Resource' folder and set build type is 'BundleResource'
 
Note
For iOS applications, we have to add file name of the fonts in 'info.plist' file additionally. Here is the sample code of adding font file names added in 'info.plist' file.
 
Sample code    
  1. <key>UIAppFonts</key>  
  2. <array>  
  3.    <string>font filename.ttf</string>  
  4. </array>  
After adding custom fonts '.ttf' files to projects, we have to create a resource object either in application level or page level for the custom fonts used in our application. 
 
Sample code
  1. <ResourceDictionary>  
  2.    <OnPlatform x:TypeArguments="x:String" x:Key="NormalFont">  
  3.       <On Platform="Android" Value="OpenSans-Regular.ttf#Open Sans" />  
  4.       <On Platform="UWP" Value="/Assets/OpenSans-Regular.ttf#Open Sans" />  
  5.       <On Platform="iOS" Value="OpenSans-Regular" />  
  6.    </OnPlatform>  
  7. </ResourceDictionary>  
Where for IOS platform we need write the exact name of the file without extension, in Android platform we need to write filename with extension. In the above we write the font's actual name after hash (#). We can find the font's actual name, you will see it when you open the downloaded font folder. Here the font name will not be the same as the font file name every time.
 
Process of using custom fonts in xaml code:
 
By using staticresource dictionary we can consume custom fonts in xaml page. 
 
Sample code 
  1. <StackLayout>  
  2.   <Label Text="Welcome to Xamarin Forms! (OpenSans-Regular)" FontFamily="{StaticResource NormalFont}" />  
  3.   <Label Text="Welcome to Xamarin Forms! (Default)" />  
  4. lt;/StackLayout>