Paddy
Paddy

Reputation: 99

Custom font not displaying on Android with Xamarin Forms and Prism

Developing an app for iOS and Android with Xamarin Forms (2.3.1) and Prism (6.2) we've had difficulty getting custom fonts to work on Android but iOS has been fine.

We have followed the most recent documentation (i.e. not using custom renders) and seem to have narrowed it to being related to Prism...

To simplify the problem we made a new Forms App, added the Lobster-Regular.ttf from the Xamarin demo to Android Assets and iOS Resources and ensured its property was AndroidAsset and BundleResource respectively, then in the default xaml page added:

    <Label Text="Hello Forms with XAML">
        <Label.FontFamily>
            <OnPlatform x:TypeArguments="x:String">
                <OnPlatform.iOS>MarkerFelt-Thin</OnPlatform.iOS>
                <OnPlatform.Android>Lobster-Regular.ttf#Lobster-Regular</OnPlatform.Android>
                <OnPlatform.WinPhone></OnPlatform.WinPhone>
            </OnPlatform>
        </Label.FontFamily>
    </Label> 

(That's also straight from the demo)

It works for both iOS and Android.

If we create a new Prism Unity App, add the font file, check the properties (which default correctly) and insert the label in MainPage.xaml iOS will use the custom font but Android will just use the standard font.

There is a known issue with custom fonts in UWP and WP apps but this doesn't seem related.

All testing has been on the iOS simulator and the Android Emulator.

Update As per Dan S.'s comment I've uploaded a project to show a minimal prism app with the custom font that works in iOS but not Android: sample project

Upvotes: 1

Views: 1720

Answers (3)

I was getting this error too, what I did was only.

In the property of the .ttf file, in compilation actions I set as embedded resource. And it works for me.

By the way, apologies for possible spelling errors. I dont speak english very well...

Upvotes: 0

Mitselplik
Mitselplik

Reputation: 1133

This problem was driving me insane because no matter what I tried, the font would show up in my UWP application, but not in my Android application.

I found the solution however. As stupid as this is, if the font file itself is named "MyFont.TTF" and not "MyFont.ttf" (note the difference in capitalization of the file extension), no errors occur in the Android app, but the font doesn't show up. When I changed the case of the file extension to lower case, rebuilt, and redeployed, as if by magic, the font showed up!

Hope someone else reads this post and it helps them get over this silly hurdle...

Upvotes: 1

Dan Siegel
Dan Siegel

Reputation: 5799

Based on the solution you provided, the problem appears to be with the version of Xamarin Forms you are using in your project. Your sample uses X.F 2.3.1.114, the sample from Xamarin uses 2.3.3.180. Updating to at least 2.3.3.180 resolves the issue in your sample. Updating to Prism 6.3.0-pre2 also works (but only because it updates Xamarin Forms to the latest release).

Upvotes: 1

Related Questions