I've updated a font within a MAUI app, but it only loads on Android and not iOS with no errors appearing in the Application console. This occurs both on the simulator and on devices.
The app runs whilst in debug on iOS without the setup fonts, but crashes when run in release mode. The simulators console doesn't seem to give any indication as to why the crash happens, and nothing jumps when looking through the build log with the Structured Log Viewer/
I've used the fontbook on Mac to verify the font, installed it successfully on the laptop, and deleted some build / cache things:
Interestingly, after I spent time looking into this I swapped the orignal font back into the project. . . and that didn't work either. Resetting the branch back to a know working state did.
I have confirmed that the font works when adding it in the way described below into a new project.
Any help with pinning down why this font won't work, how to debug further, or an alternative way to load the fonts, would be much appreciated. Thanks is advance.
Font's are dragged and dropped into the Visual Studio Mac interface:
These font's are loaded into the app in .csproj
via the following, plus an un-shown attempt to explicitly add each font:
<ItemGroup>
<MauiFont Include="Resources\Fonts\*" />
</ItemGroup>
Any auto added <None Remove=". . . " />
statements are deleted from the file, and after setting the build action to MAUIFont, and "copy to output directory", the following is added to the .csproj
for each of the fonts.
<ItemGroup>
<MauiFont Update="Resources\Fonts\Manrope-Regular.ttf">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</MauiFont>
</ItemGroup>
These fonts are then loaded into the app via MauiProgram.cs
:
.ConfigureFonts(fonts => {
fonts.AddFont("Manrope-Regular.ttf", "SecondaryRegular");
fonts.AddFont("Manrope-Bold.ttf", "SecondaryBold");
fonts.AddFont("Manrope-ExtraBold.ttf", "SecondaryBlack");
foreach (var f in fonts)
{
Debug.WriteLine($"Font: {f.Filename} / {f.Alias}");
}
});
The debug statement prints:
Font: Manrope-Regular.ttf / SecondaryRegular
Font: Manrope-Bold.ttf / SecondaryBold
Font: Manrope-ExtraBold.ttf / SecondaryBlack
The font is used throughout the app from Resources/Raw/Styles.xaml
, for example:
<Style TargetType="Button" x:Key="FullWidthButton">
<Setter Property="FontFamily" Value="SecondaryRegular"/>
</Style>
This is the point at which I'd expect this to work, and did so with other fonts previously. And it does with this one on Android, but not on iOS.
From Platforms/iOS/AppDelegate.cs
, I can check if the font's are available to the app once it's fully loaded.
public override bool FinishedLaunching(UIApplication app, NSDictionary options)
{
foreach (var item in UIFont.FamilyNames)
{
Debug.WriteLine($"Font Family: {item}");
Debug.WriteLine($"Font Names: {string.Join(", ", UIFont.FontNamesForFamilyName(item))}");
}
}
Which prints out:
Font Family: Manrope
Font Names: Manrope-Regular, Manrope-Bold, Manrope-ExtraBold
I can also instantiate a new label using the font from platform code, at which point I'd expect some complaints in the Debug Console.
public override bool FinishedLaunching(UIApplication app, NSDictionary options)
{
var font = UIFont.FromName("Manrope-Regular", 16);
Debug.WriteLine($"Font: {font}"); // Prints out "Font: Manrope-Regular 16"
var label = new UILabel
{
Text = "Hello, Maui!",
Font = font,
. . .
};
Debug.WriteLine($"Font: {font}");
}
For fun, I also added the font directly to the Platforms/iOS/Info.plist
file with no joy, but I didn't expect that this was needed anyway.
<key>UIAppFonts</key>// OR //<key>Fonts provided by application</key>
<array>
<string>Manrope-Bold.ttf</string>
<string>Manrope-Regular.ttf</string>
<string>Manrope-ExtraBold.ttf</string>
<string>CrimsonProBold.ttf</string>
<string>CrimsonProRegular.ttf</string>
</array>
I got this working . . . without really doing anything different to my post besides not using VS Mac. The steps I followed were:
I'm really not sure why this didn't work first bunch of times, but am putting it down to VS MAC getting confused and a hidden cache somewhere in the toolchain.