views:

2587

answers:

2

Why is it so hard to figure out how to draw Unicode characters on the iPhone, deriving simple font metrics along the way, such as how wide each imaged glyph is going to be in the font of choice?

It looks like it'd be easy with NSLayoutManager, but that API apparently isn't available on the phone. It appears the way people are doing this is to use a private API, CGFontGetGlyphsForUnichars, which won't get you past the Apple gatekeepers into the App store.

Can anybody point me to documentation that shows how to do this? I'm losing hair rapidly.

Howard

+2  A: 

I assumed that the exclusion of CGFontGetGlyphsForUnichars
was an oversight rather than a deliberate move, however I'm not
betting the farm on it. So instead I use

[NSString drawAtPoint:withFont:]; (in UIStringDrawing.h)

and

[NSString sizeWithFont];

This also has the advantage of performing decent substitution
on characters missing from your font, something that
CGContextShowGlyphs does not do.

Rhythmic Fistman
You know: that's almost it. That takes care of my need to image the character, but not to get simple font metrics for it. Turns out tho that you can jump into and out of the current context (`UIGraphicsGetCurrentContext`) and use `CGContextGetTextPosition` before and after drawing to essentially get the width of the bounding box. That's mostly what I'm interested in deriving, so thank you!
hkatz
Wait, what's wrong with `[@"a" sizeWithFont:<font>]`?Why do you need metrics for individual chars?
Andrew Pouliot
It gives me metrics because I do a sizeWithFont for each character.Andrew: Perhaps he wants to cache the characters to a texture, or draw them along a path.
Rhythmic Fistman
A: 

I've made a pretty suitable replacement for the private function. Read about it here: http://thoughts.codemelody.com/2009/07/a-replacement-for-cgfontgetglyphsforunichars/

James