繁体   English   中英

使用MonoTouch和CoreGraphics渲染自定义字体

[英]Rendering Custom Fonts Using MonoTouch & CoreGraphics

我需要能够在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);
}

这样可行:

    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.SelectFont("Helvetica-Bold", textHeight, CGTextEncoding.MacRoman);
        //var cgFont = CGFont.CreateWithFontName("Cooper Black");
        //context.SetFont(cgFont);

        context.SelectFont("Cooper Black", 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);
    }

我如何使用

...

public class CustomView: UIView
{
    public CustomView ()
    {
    }

    public override void Draw (System.Drawing.RectangleF rect)
    {
        base.Draw (rect);

        var context = UIGraphics.GetCurrentContext();
        DrawCenteredTextAtPoint(UIGraphics.GetCurrentContext(), 50, 10, "adsasdasd", 20);
    }

...

    public override void ViewDidLoad ()
    {
        base.ViewDidLoad ();

        v = new CustomView();
        v.BackgroundColor = UIColor.Red;
        View.AddSubview(v);
        v.Frame = new RectangleF(0, 100, 320, 100);

...

Cooper Black字体的外观:

在此处输入图片说明

请注意,您应该将字体正确添加到项目中:

  • 将字体文件引用添加到cproj文件;
  • Build Action中将字体文件标记为Content ;
  • 将字体文件引用添加到Info.plist文件

注意: http : //fastchicken.co.nz/2012/09/07/using-fonts-in-a-monotouch-app/

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM