Custom font does not render correctly when using 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 us开发者_如何学Pythoning 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!
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.
精彩评论