> I'm not really sure about this, but isn't the line hight the distance
> between two base lines? That would be more than the actual height of
> your text.
that's an interesting theory. the method i showed was just what i *thought*
should work. the method i've actually been using, which gets better results,
subtracts 1.0 from the y coord before drawing. it could be that the linegap
is about 1.0 for the fonts that look good, and something substantially
different for the fonts that look bad.
> Anyways defaultLineHeightForFont is deprecated:
>
> "This method is deprecated. Use NSLayoutManager's
> defaultLineHeightForFont: instead. Equivalent to ascent plus descent
> plus linegap."
what if you don't have an NSLayoutManager handy? as far as i'm aware, i don't
have one in any of the situations i normally draw in.
also, you must be looking at the 10.4 docs? i'm still on 10.3. in my set,
the docs for [NSFont defaultLineHeightForFont] says it returns the exact same
thing: ascent plus descent plus linegap.
anyway, they really should have a cocoa method that does this for you, if it's
going to be so complicated.
Patrick Machielse - 26 Aug 2005 14:26 GMT
Jøhnny Fävòrítê <brunsona@newsguy.takethisout.com> wrote:
> anyway, they really should have a cocoa method that does this for you, if it's
> going to be so complicated.
The scoop on font metrics is at:
<http://developer.apple.com/documentation/Cocoa/Conceptual/FontHandling/
index.html#//apple_ref/doc/uid/10000093i>
but you really just want to use:
- (NSSize)sizeWithAttributes:(NSDictionary *)attributes
which is part of the NSString Additions category defined by AppKit:
<http://developer.apple.com/documentation/Cocoa/Reference/ApplicationKit
/ObjC_classic/Classes/NSStringAdditions.html#//apple_ref/doc/uid/2000015
5-BAJEIFEA>
I grant you that finding methods defined in categories is sometimes a
bit difficult.
patrick
matt neuburg - 26 Aug 2005 15:26 GMT
Jøhnny Fävòrítê <brunsona@newsguy.takethisout.com> wrote:
> what if you don't have an NSLayoutManager handy?
Make one. The docs tell you how to create the text architecture in code.
I always do this when laying out text for drawing -- in order to know
how text will wrap in a rectangle, for example. Also there's the
excellent TipWrapper example.
> anyway, they really should have a cocoa method that does this for you
The facts about font metrics are not hard. On the other hand there is
some question of what vertically centered would really mean, so a lot of
this pure cosmetics. What I do is just sizeToFit so I know the width and
height of the text's rectangle and just stick that rectangle where I
think it should go. m.

Signature
matt neuburg, phd = matt@tidbits.com, http://www.tidbits.com/matt/
Tiger - http://www.takecontrolbooks.com/tiger-customizing.html
AppleScript - http://www.amazon.com/exec/obidos/ASIN/0596005571
Read TidBITS! It's free and smart. http://www.tidbits.com