views:

321

answers:

1

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, kCGEncodingFontSpecific);

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.

Thanks

A: 

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.

Felixyz
Thanks for the link. I looked at this about a week ago, and it's doesn't really fit my needs; it's much more complex, and those methods in particular seem to be much more suited for dynamic sizing. I don't need to glyphs to change to match a rectangle, I need a rectangle for the glyph!
Chris
Check out sizeWithZFont: in the FontLabelStringDrawing class.
Felixyz