Skip to main content

Register Fonts

  • 2 minutes to read

This topic explains how to register fonts used in MAUI apps with Reporting included.

Add TTF files that contain your custom fonts to the project’s Resources/Fonts folder. Refer to the following article for more information on fonts in .NET MAUI: Fonts in .NET MAUI.

The Reporting API requires you to add fonts to the DevExpress.Drawing.DXFontRepository class so you use them in a report. Refer to the following article for more information: Use DXFontRepository to Add Custom Fonts.

To work with fonts or other graphics entities in your reports, make sure that you add the following NuGet packages to your project:

The code snippets below add the Lobster and OpenSans-Regular (distributed by default) fonts to the DXFontRepository.

For the Android platform, add the following code lines to the MainApplication class (located in “Platforms/Android”):

public class MainApplication : MauiApplication {
    public MainApplication(IntPtr handle, JniHandleOwnership ownership)
        : base(handle, ownership) {
    }
    protected override MauiApp CreateMauiApp() => MauiProgram.CreateMauiApp();
    public override void OnCreate() {
        RegisterFont("Lobster-Regular.ttf");
        RegisterFont("OpenSans-Regular.ttf");
        base.OnCreate();
    }
    void RegisterFont(string fontName) {
        using (StreamReader rd = new StreamReader(Assets.Open(fontName))) {
            using (var ms = new MemoryStream()) {
                rd.BaseStream.CopyTo(ms);
                DXFontRepository.Instance.AddFont(ms);
            }
        }
    }
}

For the iOS platform, add the following code lines to the AppDelegate class (located in “Platforms/iOS”):

public class AppDelegate : MauiUIApplicationDelegate {
    protected override MauiApp CreateMauiApp() => MauiProgram.CreateMauiApp();
    public override bool FinishedLaunching(UIApplication application, NSDictionary launchOptions) {
        RegisterFont("Lobster-Regular.ttf");
        RegisterFont("OpenSans-Regular.ttf");
        return base.FinishedLaunching(application, launchOptions);
    }
    void RegisterFont(string fontName) {
        var bundlePath = Foundation.NSBundle.MainBundle.BundlePath;
        var fontPath = Path.Combine(bundlePath, fontName);
        using (MemoryStream ms = new MemoryStream(File.ReadAllBytes(fontPath))) {
            DXFontRepository.Instance.AddFont(ms);
        }
    }
}

After a font is added to the DXFontRepository, you can use it to configure texts in reports:

public XtraReport CreateReport() {
    XtraReport report = new XtraReport() { Name = "Sample" };
    DetailBand detail = new DetailBand();
    XRLabel labelLobster = new XRLabel() {
        Text = "Sample Text Lobster",
        Font = new DevExpress.Drawing.DXFont("Lobster", 24f),
        CanGrow = true,
        SizeF = new System.Drawing.SizeF(report.PageWidth - report.Margins.Left - report.Margins.Right, 200),
    };
    detail.Controls.Add(labelLobster);
    report.Bands.Add(detail);
    report.CreateDocument();
    return report;
}

To use a custom font in the MAUI part of the application, you still need to call the FontsMauiAppBuilderExtensions.ConfigureFonts method in the MauiProgram class:

public static class MauiProgram {
    public static MauiApp CreateMauiApp() {
        ThemeManager.ApplyThemeToSystemBars = true;
        var builder = MauiApp.CreateBuilder();
        builder
            //...
            .ConfigureFonts(fonts => {
                fonts.AddFont("Lobster-Regular.ttf", "Lobster");
            });
        return builder.Build();
    }
}