我需要能够在 CGContext 中绘制自定义字体,但是当我更改选择字体的方式时,不会显示字体。
//Works
protected void DrawCenteredTextAtPoint(CGContext context, float centerX, float y, string text, int textHeight)
{
context.SelectFont("Helvetica-Bold", textHeight, CGTextEncoding.MacRoman);
context.SetTextDrawingMode(CGTextDrawingMode.Invisible);
context.ShowTextAtPoint(centerX, y, text, text.Length);
context.SetTextDrawingMode(CGTextDrawingMode.Fill);
context.ShowTextAtPoint(centerX - (context.TextPosition.X - centerX)/2, y, text, text.Length);
}
//Does not work
protected void DrawCenteredTextAtPoint(CGContext context, float centerX, float y, string text, int textHeight)
{
var fontPath = NSBundle.MainBundle.PathForResource("COOPBL", "ttf", "fonts", "");
var provider = new CGDataProvider(fontPath);
var font = CGFont.CreateFromProvider(provider);
context.SetFont(font);
context.SetTextDrawingMode(CGTextDrawingMode.Invisible);
context.ShowTextAtPoint(centerX, y, text, text.Length);
context.SetTextDrawingMode(CGTextDrawingMode.Fill);
context.ShowTextAtPoint(centerX - (context.TextPosition.X - centerX)/2, y, text, text.Length);
}