When you create a MAUI application, the default application’s MauiProgram.cs contains the following
builder .UseMauiApp<App>() .ConfigureFonts(fonts => { fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular"); fonts.AddFont("OpenSans-Semibold.ttf", "OpenSansSemibold"); });
As can be seen, we’re using .AddFont to add .ttf filename along with an optional alias.
The .ttf files are stored in the Resources/Fonts folder or our application’s project and ofcourse the file name’s match those used in .AddFont
To use our font’s within the MAUI XAML we simply use the alias name like this
<Setter Property="FontFamily" Value="OpenSansRegular"/>
This is taken from the default MAUI application’s Styles.xaml file.
Adding our own font
This all looks fairly simple, so let’s add a font from Google Fonts.
I’m going to download the DynaPuff font, as I want something that will stand out when its changed.
- Download your font choice
- From the zip I opened static\DynaPuff and copied DynaPuff-Regular.ttf into my application’s Resources/Fonts folder
- Within MauiProgram.cs I added the following to the ConfigureFonts method
fonts.AddFont("DynaPuff-Regular.ttf", "DynaPuff");
- To test this, I changed a XAML Label element (the Hello, World label in the default generated application) to add this
FontFamily="DynaPuff"
And that’s it, really simple.