Re: Centering text drawn to screen
Re: Centering text drawn to screen
- Subject: Re: Centering text drawn to screen
- From: Chris Giordano <email@hidden>
- Date: Thu, 13 Jun 2002 16:20:29 -0400
Ryan,
I was just doing something similar, so this is still fresh in my mind.
(Or at least the project is still open in Project Builder.)
The general idea is this: you have a rect that you want to draw the
string in the middle of. So, from the center of this rect, you move
left half of the width of the string you're drawing, and draw starting
there.
So, given an NSRect, bounds, which are -- I believe -- the screen
coordinates that I'm drawing in, and given a NSStrin, label, I first
find the size of the string, and then move left from the center one half
the width of the string. You could do something similar with the Y
coordinate if you wanted -- here I'm just drawing 2.0 above the bottom
of the bounds.
NSSize labelSize;
NSPoint labelPoint;
NSDictionary labelAttributes = [NSDictionary
dictionaryWithObjectsAndKeys:[NSFont toolTipsFontOfSize:11.0],
NSFontAttributeName, nil];
labelSize = [label sizeWithAttributes:labelAttributes];
labelPoint = NSMakePoint(rint(NSMidX(bounds) - (labelSize.width *
0.5)), 2.0);
[label drawAtPoint:labelPoint withAttributes:labelAttributes];
DISCLAIMERS: I'm by no means an expert at this. I've read a bit, and
experimented a bit more. I've got something working, so I'm doing
something that at least appears correct. The code above is based on
that. YMMV, IANAL, please keep hands and feet inside the vehicle at all
times, objects may be closer than they appear, etc.
Hope this helps.
chris
On Thursday, June 13, 2002, at 03:54 PM, Ryan McPherson wrote:
Hello,
I am writing a screensaver that flashes text on the screen using
NSString's
drawInRect:withAttributes: method. I have been successful so far, but
the
text is drawn left-adjusted to the rectangle I'm drawing in and I want
it to
be centered. I couldn't find an attribute that handles this in the
documentation.
Any suggestions?
Thanks in advance!
-Ryan
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.