Is there a pattern for using custom fonts from a class library? #10762
-
I'm trying to create a class library with a shared media transport control want to use a symbol font for the transport controls but I'm stuck on finding the right way to register the font. Everything I've found shows registering fonts using MauiApp.CreateBuilder(). What's the right way to do this from a class library? |
Beta Was this translation helpful? Give feedback.
Replies: 5 comments 5 replies
-
FWIW: The solution I came up with is to use IFontCollection.AddEmbeddedResourceFont(assembly, filename, alias). ... In my satellite assembly, I define a FontResource class and statically initialize a list of font resources. List defaults = new List() In the application's MauiProgram... .ConfigureFonts(fonts => |
Beta Was this translation helpful? Give feedback.
-
Since I'm using the same fonts across all applications, I ended up moving the fonts to the assembly containing the FontResource type, set the Build Action to MauiFont, and defined a static IEnumerable property. FontResource.Load enumerates the array and calls font.AddFont(...) or font.AddEmbeddedResourceFont depending on the value of FontResource.Assembly. NOTE: Since all fonts are now in the satellite assembly, I no longer call AddEmbeddedResourceFont. In hindsight, it would have been nicer if AddFont had an (Assembly, string, string) overload to support MauiFont from any assembly. ... In the MauiAppBuilder... builder.ConfigureFonts(fonts => |
Beta Was this translation helpful? Give feedback.
-
Try this In the class library set build action to Then in the Maui Project:
|
Beta Was this translation helpful? Give feedback.
-
I've created at sample app at FontResource. I'm building it explicitly against 7.0.92 and no longer see any issues with loading the fonts from the main application and the shared assembly. In fact, I don't appear to need an assembly:FontExport or AddEmbeddedResourceFont. Perhaps something in Maui has changed since I originally tried to have fonts span multiple assemblies. For the sample, FontResource defines a static IEnumerable DefaultFonts property which defines in the Shared assembly. The main assembly merges the defaults with the font it defined - see the MainProgram.Fonts property. Within MainProgram.CreateMauiBuilder, I pass the merged IEnumerable to FontResource.Load. ... In practice, I use the same fonts in all of my apps so I define them all in the shared assembly. |
Beta Was this translation helpful? Give feedback.
-
I found an easy way to do this somewhere else. I just add it for whoever stumbles over this question and answers here(like me) Just put your font in the class library project in some folder. Original source: https://learn.microsoft.com/en-us/answers/questions/1011112/share-fonts-in-maui |
Beta Was this translation helpful? Give feedback.
This did the trick for me! Thank you so much @matmork. I was missing the ExportFont tags.
For the record, the whole procedure that works for me to add fonts in a MAUI library is the following:
[assembly: ExportFont("FontAwesome5Brands.otf", Alias = "FontAwesomeBrands")]
[assembly: ExportFont("FontAwesome5Solid.otf", Alias = "FontAwesomeSolid")]
[assembly: ExportFont("FontAwesome5Regular.otf", Alias = "FontAwesomeRegular")]
You don't…