canvas.Font do not work well on android and windows, but it is available on ios

Fei Xu 490 Reputation points
2023-08-24T13:28:05.6633333+00:00

Use canvas.DrawString and set canvas.Font , It work well on ios but not display right on android and windows.

<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="MauiApp2.MainPage">

    <ScrollView>
        <VerticalStackLayout
            Spacing="25"
            Padding="30,0"
            VerticalOptions="Center">
            <Label x:Name="labFont" HorizontalOptions="Center"/>
            <GraphicsView x:Name="graFont" HeightRequest="100" WidthRequest="100"/>
       
        </VerticalStackLayout>
    </ScrollView>

</ContentPage>
public partial class MainPage : ContentPage
{
	public MainPage()
	{
		InitializeComponent();
        labFont.FontFamily = "zSign004";
        labFont.FontSize = 50;
        labFont.Text = "\u0049";
        graFont.Drawable = new FontDrawable();
    }
}
internal class FontDrawable : IDrawable
{
    public void Draw(ICanvas canvas, RectF dirtyRect)
    {
        canvas.FontColor = Colors.Black;
        canvas.FontSize = 50;
        canvas.Font = new Microsoft.Maui.Graphics.Font("zSign004");
        canvas.DrawString("\u0049", 0, 0, dirtyRect.Width, dirtyRect.Height, HorizontalAlignment.Center, VerticalAlignment.Center);

    }
}
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("zSign004.ttf", "zSign004");
            });

#if DEBUG
		builder.Logging.AddDebug();
#endif

		return builder.Build();
	}
}

微信截图_20230824211041

微信截图_20230810192206

微信截图_20230824210938

微信截图_20230824211003

If you set font OpenSans-Semibold and canvas.drawstring some string ,it will display nobold on android and windows, ios is ok.

.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
3,541 questions
{count} votes

Accepted answer
  1. Rob Caplan - MSFT 5,452 Reputation points Microsoft Employee
    2023-08-28T07:02:25.0733333+00:00

    This appears to be a known issue with Canvas.DrawString: Can't consume a custom font in GraphicsView #9252

    One of your graphics shows the glyph “I” being displayed, matching the requested string “\u0049”. This suggests the right character is drawn but in the default font not the desired font.

    There is a workaround to use different font management to draw the string in the GitHub issue, and you can track the issue on GitHub to find when it is fixed. It is currently targeted for a .Net 8 servicing release after the .Net 8 GA.

    1 person found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.