NSString drawAtPoint and vertical font alignment
NSString drawAtPoint and vertical font alignment
- Subject: NSString drawAtPoint and vertical font alignment
- From: Stephen Blinkhorn <email@hidden>
- Date: Sun, 6 Sep 2009 18:11:06 -0600
Hi all,
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];
_______________________________________________
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