开发者

iPhone SDK Quartz Glyph Drawing

I am trying to render font glyphs at a specific point with some precision, and am having quite a bit of trouble with it.

To begin with, the Text Position Y value never changes, so I cannot get the glyph's height at all. For example, in the following code, the assertion always fires:

CGContextSelectFont(g, "Arial", 32.0f, kCGEncodin开发者_开发技巧gFontSpecific);

char c = 'A';
CGPoint pt = CGPointMake(400, 400);

CGContextSetTextDrawingMode(g, kCGTextInvisible);
CGContextSetTextPosition(g, pt.x, pt.y);
CGContextShowText(g, &c, 1);

CGPoint pt2 = CGContextGetTextPosition(g);
assert(pt2.y != pt.y);

I have tried to work around this using some hard-coded width-to-height ratios, assuming the glyphs scale linearly. Note that the X value of the text position is updated correctly.

More importantly, however, is how the glyph renders in relation to the Text Position; some glyphs render with the Y value as the center of the glyph, some use the Y value as the top of the glyph, and others use the Y value as the bottom of the glyph. I can't hard-code all this data and hope to get my app done any time this year.

Is there anything I am doing incorrectly in the drawing code above? I can't find any reference in the Quartz docs related to vertical alignment, and am a little lost.


From what I've gleamed this is a fairly complex task. I'd check out zynga's LabelFont library and either learn from their code or simply use the library. Especially have a look at the LabelFont class' drawTextInRect: method.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜