Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion Groups
General
GeneralPortable MacsHardwareNetworking
Applications
Mac ApplicationsEudoraFirefox / MozillaInternet ExplorerOutlook ExpressMS OfficeEntourageExcelPowerPointWordVirtual PCMedia PlayerOther MS Products
Programming
Mac ProgrammingCodeWarriorPerl
Country Specific
Australian Mac GroupUK Mac Group

Mac Forum / Programming / Mac Programming / August 2005



Tip: Looking for answers? Try searching our database.

vertical font centering in cocoa

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Jhnny Fvrt (it means "wa wa dance") - 26 Aug 2005 03:16 GMT
i want to vertically center a line of text inside a rect, and draw it in a
certain font.

 -(void)drawString:(NSString*)string
  inRect:(NSRect)rect font:(NSFont*)font
   {
     NSDictionary*  attr = nil;
     float          hght = [font defaultLineHeightForFont];
     NSPoint        nspt = rect.origin;

     nspt.y += ceil((rect.size.height - hght) / 2.0);

     attr = [NSDictionary dictionaryWithObjectsAndKeys:
      font, NSFontAttributeName, nil];

     [string drawAtPoint:nspt withAttributes:attr];
   }

i would think that would be the obvious way to go about it, and it looks fine
for a lot of fonts.  but i'd say maybe half have issues, where they appear too
low or too high within the rect.  is there some other algorithm i could try
that would work better in the general case?
Patrick Machielse - 26 Aug 2005 09:30 GMT
Jøhnny Fävòrítê <brunsona@newsguy.takethisout.com> wrote:

> i want to vertically center a line of text inside a rect, and draw it in a
> certain font.
[quoted text clipped - 18 lines]
> low or too high within the rect.  is there some other algorithm i could try
> that would work better in the general case?

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.

Anyways defaultLineHeightForFont is deprecated:

"This method is deprecated. Use NSLayoutManager's
defaultLineHeightForFont: instead. Equivalent to ascent plus descent
plus linegap."

I _believe_ the text height is equivalent to ascent + descent.

patrick
Jhnny Fvrt (it means "wa wa dance") - 26 Aug 2005 10:47 GMT
> 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

 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.