Reputation: 75
I'm experiencing problems using a font in MAUI. The project in which the font is used is very very simple (the general Hello World!). The font is placed in the correct directory (Resources/Font), is placed in the MauiProgram.cs but the font doesn't display. Does anybody know why?
namespace MauiTest;
public static class MauiProgram
{
public static MauiApp CreateMauiApp()
{
var builder = MauiApp.CreateBuilder();
builder
.UseMauiApp<App>()
.ConfigureFonts(fonts =>
{
fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
fonts.AddFont("OpenSans-Semibold.ttf", "OpenSansSemibold");
fonts.AddFont("Creamer.ttf", "MyFont");
});
return builder.Build();
}
}
I'm using Visual Studio 2022 version 17.4.3. Many thanks!
Upvotes: 2
Views: 4200
Reputation: 45
It is not applicable to your case, but someone might find this helpful. The default MAUI app template include font files with spaces in the file name (for example "OpenSans Regular.ttf") substituting the spaces with "-" in the file name and its reference in MauiProgram.cs will solve the problem.
Upvotes: 0
Reputation: 1
I had some similar experience: Sometimes everything worked fine, sometimes it doesn't. After trying the above solution, it worked just like a direction signal on my car: working, not working, working.
Then I just opened the project file and changed Update
into Include
:
<ItemGroup>
<MauiFont Include="Resources\Fonts\digital7.ttf">
</MauiFont>
<MauiFont Include="Resources\Fonts\MaterialDesignIcons.ttf">
</MauiFont>
</ItemGroup>
Now, everything works like expected.
Upvotes: 0
Reputation: 31
I was facing the same problem. For me, it worked by changing the Build Action property to MauiFont and Copy to Output Directory to Copy Always.
Upvotes: 2
Reputation: 77
I didnt try the 1st answer in Behnam's response but I did try the second which didnt work and gave me the same
@(Content) build action is not supported
.
So after a quick google of Maui fonts to find a new font, I instead
I compared the changes before and after and my font did go back to how I wanted it. Hopefully this answer helps someone else.
Upvotes: 4
Reputation: 66
It is possible that there is a compatibility issue with the font you are trying to use. Can you try using a different font to see if it works? If that doesn't solve the issue, you could try adding the font to the system and then referencing it in your code.
Another possibility could be that the font file is not being included in the build output. You can check this by right-clicking the font file in Visual Studio and selecting "Properties." Ensure that the "Build Action" property is set to "Content" and the "Copy to Output Directory" property is set to "Copy if newer."
Upvotes: 3