C# 使用MonoTouch&;渲染自定义字体;岩芯图
我需要能够在CGContext中绘制自定义字体,但当我更改选择字体的方式时,字体不会显示C# 使用MonoTouch&;渲染自定义字体;岩芯图,c#,xamarin.ios,core-graphics,C#,Xamarin.ios,Core Graphics,我需要能够在CGContext中绘制自定义字体,但当我更改选择字体的方式时,字体不会显示 //Works protected void DrawCenteredTextAtPoint(CGContext context, float centerX, float y, string text, int textHeight) { context.SelectFont("Helvetica-Bold", textHeight, CGTextEncoding.MacR
//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);
}
这是有效的:
受保护的void DrawCenteredTextAtPoint(CGContext上下文、float centerX、float y、字符串文本、int text高度)
{
//var fontPath=NSBundle.MainBundle.PathForResource(“COOPBL”、“ttf”、“font”和“);
//var provider=新的CGDataProvider(fontPath);
//var font=CGFont.CreateFromProvider(提供者);
//context.SelectFont(“Helvetica Bold”,textHeight,CGTextEncoding.MacRoman);
//var cgFont=cgFont.CreateWithFontName(“库珀黑”);
//context.SetFont(cgFont);
context.SelectFont(“Cooper Black”、textHeight、CGTextEncoding.MacRoman);
SetTextDrawingMode(CGTextDrawingMode.Invisible);
context.ShowTextAtPoint(centerX,y,text,text.Length);
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
font时的外观:
请注意,您应该正确地将字体添加到项目:
- 向cproj文件添加字体文件引用李>
- 在
中将字体文件标记为构建操作
李>内容
- 将字体文件引用添加到
注意:。您是否尝试在更简单的地方使用自定义字体?例如,调用
UILabel.Font=UIFont.FromName(fn,17)代码>,其中fn=“COOPBL”。我问的原因是,当您在Xamarin Studio中将字体添加到cproj文件时,它不会在构建操作中标记为内容。所以,可能字体并没有正确地包含在项目中。