Wiley25
Wiley25

Reputation: 11

C# Custom Fonts not working in Xamarin Forms (Android). Xamarin 4.8. Windows 10, VS19

I'm having an issue with Xamarin Forms 4.8 custom fonts.

I've followed the guides to the letter, that is:

-Loaded my font files (.ttf or .otf) into my shared directory and marked them as Embedded resources.

-Added the ExportFont outside of my namespace.

When I reference the font anywhere it just builds the standard font. Here is a snippet of code:

using System;
using System.Collections.Generic;
using Xamarin.Forms;
using Newtonsoft.Json.Linq;
using System.IO;
using System.Reflection;

[assembly: ExportFont("Stencil.ttf")]
[assembly: ExportFont("GreatVibes.otf")]
[assembly: ExportFont("Lobster-Regular.ttf", Alias= "MyFont")]

namespace BAAaM
{
    public partial class MainPage : ContentPage
    {

        public MainPage()
        {

            Grid topgrid = new Grid();
            topgrid.Children.Add(new Label { Text = "Test, please work", FontSize = 30, FontFamily = "Lobster-Regular" }, 0, 1);
this.Content = topgrid;
}
}
}

I've experimented placing the fonts into my Android directory, under assets but that doesn't work either. Is Xamarin overwriting whatever I do with standard Font as part of the built in label functions? I have no problems changing the text size.

Thanks,

M

Upvotes: 0

Views: 712

Answers (2)

grilljannar
grilljannar

Reputation: 11

I had this issue.

My solution: Changing file structure

FROM: Assets/Fonts/PoppinsBold.ttf

To: Fonts/PoppinsBold.ttf

Upvotes: 1

Junior Jiang
Junior Jiang

Reputation: 12723

You could refer to this official sample to check whether it works first.

Then I tested with your shared code:

    public FontPageXaml ()
    {
        InitializeComponent ();
        Grid topgrid = new Grid();
        topgrid.Children.Add(new Label { Text = "Test, please work", FontSize = 30, FontFamily = "Lobster-Regular" }, 0, 1);
        this.Content = topgrid;
    }

And it works:

enter image description here

Upvotes: 0

Related Questions