• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: NSString drawAtPoint and vertical font alignment
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: NSString drawAtPoint and vertical font alignment


  • Subject: Re: NSString drawAtPoint and vertical font alignment
  • From: Quincey Morris <email@hidden>
  • Date: Sun, 6 Sep 2009 17:37:34 -0700

On Sep 6, 2009, at 17:11, Stephen Blinkhorn wrote:

I would have thought that the code below will center text horizontally and vertically within a NSRect. Sometimes it does but it depends on the font I am using. So the below may work for Verdana but if I change to Helvetica then the vertical centering is off.

Is there a better way to do this?

Thanks,
Stephen


displayString = @"Test" viewRect = [self bounds];

attributes = [[NSMutableDictionary alloc] init];
[attributes setObject:textFont forKey:NSFontAttributeName];
[attributes setObject:textColor forKey:NSForegroundColorAttributeName];


NSSize titleSize = [displayString sizeWithAttributes:attributes];
int x_pos = (viewRect.size.width - titleSize.width) / 2;
int y_pos = (viewRect.size.height - titleSize.height);
NSPoint p = {x_pos, y_pos};
[displayString drawAtPoint:p withAttributes:attributes];

There are several things wrong with this:

-- You're not accounting for the bounds origin, so it only works if the origin happens to be (0, 0).

-- Presumably you meant "/ 2" on the y_pos calculation.

-- The documentation for "sizeWithAttributes:" says that it returns the size of the "bounding box". That could mean any of several different metrics that could result in the string being drawn a few pixels off from where you'd expect (especially in the Y direction, but also possibly in the X direction).

-- You calculate where you want the vertical *center* of this bounding box to be, but 'drawAtPoint:' puts the baseline at the specified point. The baseline is actually going to be more like 2/3 of the way down the bounding box in general.

I suspect the last one of these is the main reason for your vertical centering being off. Try using 'drawInRect:' instead, with a suitably calculated destination rect.


_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden


  • Follow-Ups:
    • Re: NSString drawAtPoint and vertical font alignment
      • From: Stephen Blinkhorn <email@hidden>
References: 
 >NSString drawAtPoint and vertical font alignment (From: Stephen Blinkhorn <email@hidden>)

  • Prev by Date: Re: Distributed Objects in Foundation Tool [adjunct]
  • Next by Date: Re: Easy Question re NSWindowController
  • Previous by thread: NSString drawAtPoint and vertical font alignment
  • Next by thread: Re: NSString drawAtPoint and vertical font alignment
  • Index(es):
    • Date
    • Thread