我有一个自定义视图,我试图在其中使用 Windows 字体 (calibri.ttf) 绘制文本。但是,我在使用 DrawString
和 DrawString
之间得到了不同的行为。 ShowText
函数。
我已将字体作为应用程序的一部分嵌入(将其添加到 UIAppFonts 列表中并将其构建操作设置为 Content),并且当我使用 <我的自定义 UIView 中的 code>DrawString 方法,例如
Code
public override void Draw (System.Drawing.RectangleF rect)
{
base.Draw (rect);
DrawString("Calibri font via DrawString", new RectangleF(10, 10, 100, 100), UIFont.FromName("Calibri", 16f));
}
Result
但是,如果我这次尝试使用 ShowText 绘制相同的文本,则看起来好像字体没有正确编码文本,或者字符映射是错误的。
代码
public override void Draw (System.Drawing.RectangleF rect)
{
base.Draw (rect);
var ctx = UIGraphics.GetCurrentContext();
ctx.SelectFont("Calibri", 16f, MonoTouch.CoreGraphics.CGTextEncoding.FontSpecific);
ctx.ShowTextAtPoint(10, 10, "Calibri font via ShowText using SelectFont");
}
结果
更新 - 这是如果我使用 MacRoman
编码而不是 FontSpecific
得到的结果:
我也尝试过手动加载字体并使用它,但后来我什么也没得到,就像它无法识别字体一样,例如
var fontPath = NSBundle.MainBundle.PathForResource("calibri", "ttf");
var provider = new CGDataProvider(fontPath);
var font = CGFont.CreateFromProvider(provider);
var ctx = UIGraphics.GetCurrentContext();
ctx.SetFont(font);
ctx.ShowTextAtPoint(10, 20, "Calibri font via ShowText using SetFont");
我知道 DrawString是UIKit & ShowText
是 CG (Quartz),所以我知道可能存在差异。然而,据我所知,DrawString 的唯一区别是它纠正了 Offset 差异的问题(CG 位于左下角/UIKit 位于左上方)。
注意 - 我遇到的根本问题是我需要使用此字体通过自定义 CALayerDelegate 将文本绘制到图层上。我无法从那里访问 DrawString
函数,因此,我可以看到绘制文本的唯一方法是通过 ShowText
。欢迎替代解决方案!
I have a custom view in which I am trying to draw text using a Windows font (calibri.ttf). However, I am getting different behaviour between using the DrawString
& ShowText
functions.
I have embedded the font as part of the app (added it to the UIAppFonts list & set it's build action to Content) and I all works fine when I use the DrawString
method from my custom UIView e.g.
Code
public override void Draw (System.Drawing.RectangleF rect)
{
base.Draw (rect);
DrawString("Calibri font via DrawString", new RectangleF(10, 10, 100, 100), UIFont.FromName("Calibri", 16f));
}
Result
However, if I attempt to draw the same text this time using ShowText
it appears as if the font isn't encoding the text correctly, or the character mapping is wrong.
Code
public override void Draw (System.Drawing.RectangleF rect)
{
base.Draw (rect);
var ctx = UIGraphics.GetCurrentContext();
ctx.SelectFont("Calibri", 16f, MonoTouch.CoreGraphics.CGTextEncoding.FontSpecific);
ctx.ShowTextAtPoint(10, 10, "Calibri font via ShowText using SelectFont");
}
Result
UPDATE - Here is what I get if I use MacRoman
encoding instead of FontSpecific
:
I have also tried loading in the font manually and using that but then I get nothing at all, it's like it doesn't recognise the font e.g.
var fontPath = NSBundle.MainBundle.PathForResource("calibri", "ttf");
var provider = new CGDataProvider(fontPath);
var font = CGFont.CreateFromProvider(provider);
var ctx = UIGraphics.GetCurrentContext();
ctx.SetFont(font);
ctx.ShowTextAtPoint(10, 20, "Calibri font via ShowText using SetFont");
I know that DrawString
is UIKit & ShowText
is CG (Quartz) so I understand that there may be differences. However, from what I gathered the only difference with DrawString was it corrected the issue with the difference in Offset (CG being at the bottom left/UIKit being at the top left).
NOTE - The underlying problem I have is I need to use this font to draw text onto a layer via a custom CALayerDelegate. I don't have access to the DrawString
function from in there, therefore, the only way I can see to draw the text is via ShowText
. Alternative solutions are welcome!
发布评论
评论(1)
这看起来确实像是一个编码问题。尝试使用
CGTextEncoding.MacRoman
而不是CGTextEncoding.FontSpecific
(即使 Arial 也不会按预期使用 FontSpecific 进行渲染)。10 月 12 日更新
a) 你的最后一个代码示例将无法工作,因为 Apple 文档明确 声明不要同时使用
SetFont
和ShowText
。报价如下:b) CGTextEncoding.MacRoman 代码适用于其他几种 iPhone 提供的字体。我开始怀疑 iOS 不支持 Calibri.ttf 字体本身。
This really looks like an encoding issue. Try using
CGTextEncoding.MacRoman
instead ofCGTextEncoding.FontSpecific
(even Arial wouldn't render, as expected, with FontSpecific).UPDATE Oct 12th
a) your last code sample won't work because Apple doc specifically states not to use
SetFont
andShowText
together. Quote follow:b) the
CGTextEncoding.MacRoman
code works with several other iPhone-supplied fonts. I'm beginning to suspect it's something about the Calibri.ttf font itself that is not supported by iOS.