How to Apply Custom Fonts in .NET MAUI

Custom Font

Adding custom fonts in .NET MAUI (Multi-platform App UI) is a powerful way to improve your app’s design and branding, making it stand out to users across all platforms, including Android, iOS, Windows, and macOS. In this guide, we’ll go through each step required to integrate custom fonts into your .NET MAUI app effectively, helping you achieve a polished, professional look.

Why use Custom Fonts in .NET MAUI?

Custom fonts provide numerous benefits.

  • Brand Consistency: Reinforce your brand identity by using fonts that align with your style.
  • Improved Readability: Choose fonts that enhance the readability and aesthetics of your app, improving the user experience.
  • Unique Design: Differentiate your app from others by using custom fonts that reflect the purpose and personality of your app.

Adding custom fonts in .NET MAUI is simple and can significantly impact your app's overall design.

Steps to add Custom Fonts in .NET MAUI

This tutorial breaks down the process into three essential steps.

  1. Add the Font File to Your .NET MAUI Project
  2. Register the Font in MauiProgram.cs
  3. Use the Custom Font in Your XAML or C# Code

Let’s go through each step in detail.

Step 1. Add the Font File to Your .NET MAUI Project

  • Download the Font: Find a .ttf (TrueType Font) or .otf (OpenType Font) file of your desired font.
  • Place the Font in the Correct Folder: Drag and drop the font file into the Resources/Fonts folder in your .NET MAUI project. This step is crucial because .NET MAUI loads custom fonts directly from this folder.
  • Example: If your font file is named MyCustomFont.ttf, place it in Resources/Fonts.

Step 2. Register the Font in MauiProgram.cs

After adding the font file to your project, the next step is to register it in MauiProgram.cs to ensure .NET MAUI recognizes it.

  • Open MauiProgram.cs in your project.
  • Locate the ConfigureFonts Method: Inside the CreateMauiApp method, find ConfigureFonts and add a new line to register your font file. Here’s how it should look:
    builder.ConfigureFonts(fonts =>
    {
        fonts.AddFont("MyCustomFont.ttf", "MyCustomFontAlias");
    });
    • "MyCustomFont.ttf" is the font file name located in Resources/Fonts.
    • "MyCustomFontAlias" is the alias name for the font, which you will use to apply the font in XAML or C#.

Step 3. Use the Custom Font in XAML or C#

With the font registered, it’s now ready to use in your UI.

Using the Font in XAML

To apply the custom font to a control in XAML, use the FontFamily property with the alias you created.

<Label Text="Hello, .NET MAUI!"
       FontFamily="MyCustomFontAlias"
       FontSize="24"
       TextColor="Black"/>

Using the Font in C#

If you prefer to set the font programmatically in C#, you can apply it like this.

Label label = new Label
{
    Text = "Hello, .NET MAUI!",
    FontFamily = "MyCustomFontAlias",
    FontSize = 24,
    TextColor = Colors.Black
};

Tips for Successful Font Integration

  • Check File Names: Ensure that the font file name in MauiProgram.cs matches the actual file name, including case sensitivity.
  • Confirm Licensing: Verify the font’s licensing if you’re using it commercially to avoid copyright issues.
  • Compatibility: Test the font across all supported platforms to make sure it appears correctly on Android, iOS, Windows, and macOS.

Benefits of using Custom Fonts in .NET MAUI

Custom fonts bring a range of benefits.

  • Consistent Appearance: Once set up, the font will appear consistently across all platforms.
  • Enhanced User Experience: A well-chosen font can make your app more user-friendly and visually appealing.
  • Cross-Platform Design: MAUI makes it easy to use custom fonts without additional platform-specific coding, simplifying cross-platform development.

Wrapping Up

Custom fonts can transform the look and feel of your .NET MAUI app, making it more engaging and unique. By following these steps, you’ll be able to easily add and apply custom fonts across all pages in your app. This customization not only enhances user experience but also boosts your app's professionalism and appeal. With .NET MAUI, you can bring your app's design to life with fonts that make an impression! Start experimenting with different fonts and see how they can elevate your app's style.


Similar Articles