how to use my font in UIGraphics in MAUI?

mc 5,186 Reputation points
2025-02-06T13:00:53.0866667+00:00
.ConfigureFonts(fonts =>
{
    fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
    fonts.AddFont("OpenSans-Semibold.ttf", "OpenSansSemibold");
});


how to use this font in UIGraphics in ios?

new CTFont("OpenSansRegular", fontSize);

.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
4,014 questions
0 comments No comments
{count} votes

Accepted answer
  1. Leon Lu (Shanghai Wicresoft Co,.Ltd.) 80,931 Reputation points Microsoft External Staff
    2025-02-07T06:56:34.1033333+00:00

    Hello,

    how to use this font in UIGraphics in ios?

    You can use CTFont to create a font and set this font in the CTStringAttributes with UIGraphics to draw text in a custom UIView like following code.

    using CoreGraphics;
    using CoreText;
    using Foundation;
    using UIKit;
     
    public class CustomView : UIView
    {
        public override void Draw(CGRect rect)
        {
            base.Draw(rect);
     
            using (CGContext context = UIGraphics.GetCurrentContext())
            {
                // Fill the background with white
                context.SetFillColor(UIColor.White.CGColor);
                context.FillRect(rect);
     
                // Set up the font
                float fontSize = 40f; // Adjust size for better visibility
                var fontName = "Mistral"; // font name
     
                // If using a custom font, ensure it's loaded correctly
                CTFont font = new CTFont(fontName, fontSize);
     
                // Create attributed string with explicit foreground color
                var attributes = new CTStringAttributes
                {
                    Font = font,
                    ForegroundColor = new CGColor(0, 0, 0) // Black text
                };
     
                using var attributedString = new NSAttributedString("Hello, MAUI!", attributes);
     
                // Create a framesetter
                using var framesetter = new CTFramesetter(attributedString);
                using var path = new CGPath();
                path.AddRect(new CGRect(10, rect.Height - 60, rect.Width - 20, 50)); // Adjust positioning
     
                using var frame = framesetter.GetFrame(new NSRange(0, attributedString.Length), path, null);
     
                // Flip the coordinate system to draw correctly
                context.SaveState();
                context.TranslateCTM(0, rect.Height);
                context.ScaleCTM(1, -1);
     
                // Draw text
                frame.Draw(context);
     
                context.RestoreState();
            }
        }
    }
    

    Best Regards,

    Leon Lu


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


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.