Custom fonts don't load in iOS

borrelborrel USMember ✭✭
edited June 2017 in Xamarin.iOS

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 (project is attached):

1) Import the font into Resources
2) Set Build Action to "BundleResource" (mistake in the Xamarin documentation)
3) Copy to output directory: "Always Copy"

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)

Tagged:

Answers

  • Hi @NikolasBorrel ,

    I have gone through your project and I think you forget to add these fonts in Info.plist as shown in the picture below

    I hope this helps.
    Let me know if you have any doubts further

    ThankYou.

  • borrelborrel USMember ✭✭

    Adding the fonts to the Info.plist makes it possible to use the fonts in the Storyboard, but shouldn't make any difference in the UIFont.FromName(...) function.

    Adding the font names to the Info.plist actually works in the sense that the fonts can be used in the Storyboard, but at runtime, the fonts are not used.

  • borrelborrel USMember ✭✭

    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.

  • GlenRutherfordGlenRutherford AUMember ✭✭

    For the other Windows users out there that have been banging their head against their desk for the last few hours, to add fonts into the info.plist file, you have to define the correct key first, which is "UIAppFonts" and then the string array with your fonts listed. For example:

    <dict>
    
        ...
    
        <key>UIAppFonts</key>
        <array>
              <string>fonts/Gilroy-ExtraBold.otf</string>
        </array>
        </dict>
    

    Where fonts/Gilroy-ExtraBold.otf refers to the file "Gilroy-ExtraBold.otf" in the iOS project "Resources/fonts" folder. You can then use "Gilroy-ExtraBold" as the FontFamily in your Xamarin Forms project.

  • malukishtiaqmalukishtiaq Member ✭✭
    edited January 29

    I had the same issue.... Why it wasn't working for me was, I did not add fonts in
    UIAppFonts

    FontAwsome.ttf

    Info.Plist... Instead of doing that, I just added as

    <key>UIAppFonts</key>
    <string>FontAwsome.ttf</string>
    
Sign In or Register to comment.