Bryan
Bryan

Reputation: 347

Xamarin Forms Custom Font Not Working On UWP (Works on Android)

I followed the steps at https://developer.xamarin.com/guides/xamarin-forms/user-interface/text/fonts/#Using_a_Custom_Font.

On UWP I have included the font file inside Assets/Fonts/DSEG7Modern-Regular.ttf (Build Action: Content. Copy never). Android is it at Assets/DSEG7Modern-Regular.ttf (Build Action: AndroidAsset. Copy never)

Code

        <Label Text="Hello Forms with XAML">
            <Label.FontFamily>
                <OnPlatform x:TypeArguments="x:String">
                    <OnPlatform.iOS>DSEG7Modern-Regular</OnPlatform.iOS>
                    <OnPlatform.Android>DSEG7Modern-Regular.ttf#DSEG7 Modern</OnPlatform.Android>
                    <OnPlatform.WinPhone>Assets/DSEG7Modern-Regular.ttf#DSEG7 Modern</OnPlatform.WinPhone>
                </OnPlatform>
            </Label.FontFamily>
        </Label>

This works properly on an Android device. It also works properly if I install the font on my local system. If I do not have the font installed (which my users won't), it will not work.

I also tried this with code behind and it did not work either.

   private string GetDSEG7FontForCurrentDevice()
    {
        switch (Device.RuntimePlatform)
        {
            case Device.Windows:
            case Device.WinPhone:
                return "Assets/Fonts/DSEG7Modern-Regular.ttf#DSEG7 Modern";
            case Device.Android:
                return "DSEG7Modern-Regular.ttf#DSEG7 Modern";
            case Device.iOS:
                return "DSEG7Modern-Regular";
        }

        return MainLabel.FontFamily;
    }

Upvotes: 1

Views: 727

Answers (1)

Yuri S
Yuri S

Reputation: 5370

Add "/" before "Assets" like below

in xaml

    <Label x:Name="LabelModernFont" Text="Hello Forms with XAML">
        <Label.FontFamily>
            <OnPlatform x:TypeArguments="x:String" >
                <On Platform ="Windows">/Assets/Fonts/DSEG7Modern-Regular.ttf#DSEG7 Modern</On>
            </OnPlatform>
        </Label.FontFamily>
    </Label>

in code:

switch (Device.RuntimePlatform)
            {
                case "Windows":
                    LabelModernFont.FontFamily = "/Assets/Fonts/DSEG7Modern-Bold.ttf#DSEG7 Modern";
                    break;
            }

enter image description here

Upvotes: 3

Related Questions