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.