使用 ShowText 时自定义字体无法正确呈现

发布于 2024-12-08 14:09:48 字数 2134 浏览 1 评论 0 原文

我有一个自定义视图,我试图在其中使用 Windows 字体 (calibri.ttf) 绘制文本。但是,我在使用 DrawStringDrawString 之间得到了不同的行为。 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");
}

结果

使用 FontSpecific 编码和 ShowTextAtPoint 的结果

更新 - 这是如果我使用 MacRoman 编码而不是 FontSpecific 得到的结果:

Result using MacRoman编码和ShowTextAtPoint

我也尝试过手动加载字体并使用它,但后来我什么也没得到,就像它无法识别字体一样,例如

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

Using Calibri font with DrawString

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

Result using FontSpecific encoding and ShowTextAtPoint

UPDATE - Here is what I get if I use MacRoman encoding instead of FontSpecific:

Result using MacRoman encoding and ShowTextAtPoint

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

素罗衫 2024-12-15 14:09:48

这看起来确实像是一个编码问题。尝试使用 CGTextEncoding.MacRoman 而不是 CGTextEncoding.FontSpecific (即使 Arial 也不会按预期使用 FontSpecific 进行渲染)。

10 月 12 日更新

a) 你的最后一个代码示例将无法工作,因为 Apple 文档明确 声明不要同时使用SetFontShowText。报价如下:

Quartz使用系统提供的字体数据,通过当前字体的编码向量来映射数组的每个字节,从而获得要显示的字形。请注意,字体必须已使用 CGContextSelectFont 设置。不要将 CGContextShowText 与 CGContextSetFont 结合使用。

b) CGTextEncoding.MacRoman 代码适用于其他几种 iPhone 提供的字体。我开始怀疑 iOS 不支持 Calibri.ttf 字体本身。

This really looks like an encoding issue. Try using CGTextEncoding.MacRoman instead of CGTextEncoding.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 and ShowText together. Quote follow:

Quartz uses font data provided by the system to map each byte of the array through the encoding vector of the current font to obtain the glyph to display. Note that the font must have been set using CGContextSelectFont. Don’t use CGContextShowText in conjunction with CGContextSetFont.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文