Skip to content

Commit

Permalink
Merge pull request #759 from telerik/font-kb-patch
Browse files Browse the repository at this point in the history
Update troubleshooting-missing-fonts-unpackaged-deployment.md
  • Loading branch information
LanceMcCarthy authored Jan 2, 2024
2 parents b50f499 + 926c5cc commit 834ebd2
Showing 1 changed file with 51 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,57 @@ If the issue persists, check the following:
- Double-check the spelling and case sensitivity of the font file and font family names.
- Test the app on both Windows 10 and Windows Server 2019 to rule out any platform-specific issues.

If the issue still persists after following these steps, please provide a reproducible project and reach out to the Microsoft MAUI team for further investigation.
### Additional Approach

If you are still having trouble after ensuring the registration and the case-sensitive spelling is correct, there is another thing you can add to your approach. Hook into the LabelHandler's Mapper and change the native Windows font family property dynamically:

```csharp
public static MauiApp CreateMauiApp()
{
var builder = MauiApp.CreateBuilder();
builder
.UseMauiApp<App>()
.UseTelerik()
.ConfigureFonts(fonts =>
{
fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
fonts.AddFont("OpenSans-Semibold.ttf", "OpenSansSemibold");
fonts.AddFont("telerikfontexamples.ttf", "telerikfontexamples");
fonts.AddFont("telerikcontrolsicons.ttf", "telerikfontexamples");
});

#if WINDOWS10_0_17763_0_OR_GREATER
Microsoft.Maui.Handlers.LabelHandler.Mapper.AppendToMapping("FontFamily", (handler, element) =>
{
/* An unpackaged app does not have ms-appx://Assets/ folder available. To avoid issues you can explicitly define the new path to the font file
*
* - Packaged => "ms-appx://Assets/Fonts/Font.ttf#familyname"
* - Unpackaged => "font.ttf#familyname"
*
* In order for the unpackaged approach to work, be sure to include the font's file with the project's assets and set the Build Action to 'CopyAlways'
*/

handler.PlatformView.FontFamily = element.Font.Family.ToLower() switch
{
"telerikcontrolsicons" => new Microsoft.UI.Xaml.Media.FontFamily("telerikcontrolsicons.ttf#telerikcontrolsicons"),
"telerikfontexamples" => new Microsoft.UI.Xaml.Media.FontFamily("telerikfontexamples.ttf#telerikfontexamples"),
_ => handler.PlatformView.FontFamily
};
});
#endif

return builder.Build();
}
```

Note: If you are looking for the font files used in the previous code example, you can find them in the [Telerik UI for Maui Samples](https://github.com/telerik/maui-samples/tree/main/Samples/ControlsSamples/Resources/Fonts) source code. This prpblem is not specific to Telerik fonts, it can happen with any font that is not available by default.

### Further Assistance

If the issue still persists after following these steps, please provide a reproducible project and reach out to the Microsoft MAUI team for further investigation. You can get assistance in two locations:

- [.NET MAUI on GitHub](https://github.com/dotnet/maui/issues) (always search open Issues before creating a new Issue
- [Microsoft Q&A for .NET MAUI](https://learn.microsoft.com/en-us/answers/tags/247/dotnet-maui)

## Notes
- Make sure to use the correct spelling and case sensitivity when registering the font files in the `MauiProgram.cs` file.
Expand Down

0 comments on commit 834ebd2

Please sign in to comment.