1
votes

Custom fonts don't load in Xamarin iOS. As described in the Xamarin documentation from 2013 (https://blog.xamarin.com/custom-fonts-in-ios/), I have done the following:

1) Import the font into Resources
2) Set Build Action to "BundleResource" (mistake in the Xamarin documentation)
3) Copy to output directory: "Always Copy"
4) Added the font name to the array "Fonts provided by application" in the Info.plist (making the font available in the storyboard, but not at runtime)

In FinishedLaunching() in the App Delegate, I iterate through all installed fonts:

foreach (var family in UIFont.FamilyNames)
{
    System.Diagnostics.Debug.WriteLine($"{family}");

    foreach (var names in UIFont.FontNamesForFamilyName(family))
    {
        System.Diagnostics.Debug.WriteLine($"{names}");
    }
}
var fontName = UIFont.FromName("Karbon", 20.0f); // fontName is null

No custom fonts show up, what is the problem here?


Visual Studio Community 2017 for Mac (Preview) Version 7.1 Preview (7.1 build 1178) Runtime: Mono 5.2.0.179 (2017-04/4498dc4) (64-bit)

Xamarin.iOS Version: 10.12.0.5 (Visual Studio Community)

2
Have you edited your plist?Jack
I have also added the array "Fonts provided by application" in the Info.plist and added the font, which makes the font available in the storyboard. However, at runtime, the font is not loaded.borrel

2 Answers

3
votes

For anyone coming here for Windows 10 and Visual Studio

  • put xxx.ttf in Resources folder
  • Right Click on xxx.ttf > Properties > Build Action: BundleResource
  • don't do => Copy to output directory: Always Copy
  • Xamarin.iOS > Right-click on info.plist > Open With > Xml (Text) Editor
  • Add following:
<dict>

    ... ... ...

    <key>UIAppFonts</key>
    <array>
        <string>xxx.ttf</string>
    </array>

    ... ... ...

</dict>
  • in xaml use Typeface name instead of xxx.ttf file name (double click xxx.ttf to see Typeface name)
<Label.FontFamily>
    <OnPlatform x:TypeArguments="x:String">
        <On Platform="iOS" Value="Typeface_name" />
        <On Platform="Android" Value="filename_name.ttf#Typeface_name" />
        <On Platform="UWP" Value="Assets/Fonts/filename_name.ttf#Typeface_name" />
    </OnPlatform>
</Label.FontFamily>
0
votes

Update: the Info.plist has to be updated (including the font type, e.g. ttf) and the Font’s properties should be set to not copy to output directory (Do not copy). It works after these changes.