Re: Positioning a string
Re: Positioning a string
- Subject: Re: Positioning a string
- From: email@hidden
- Date: Mon, 30 Jan 2006 17:51:08 -0800
I am experiencing a little trouble with a string I want to draw in
an NSRect and as the coordinates in Cocoa are upside-down
Actually, things are finally right-side up :)
For those wanting more than a flame war, the two approaches are:
a) Top-left origin, I believe because the scan on the old CRTs
started at the top left.
b) Bottom-left origin, which is the mathematical origin.
I find (b) more intuitive as someone with a dominant mathematics &
physics background (as opposed to CRT engineering :D ), but there are
many cases where I still prefer (a) because it works out simpler for
some problems - such as the original poster's.
I am totally confused. This string has a variable height but I
want to put it always on the same point from the top of outerSize:
NSSize outerSize = [self bounds].size;
NSSize stringSize = [theString sizeWithAttributes:stringAttrib];
NSPoint stringPoint = NSMakePoint(fixedWidth, fixedHeight);
[theString drawAtPoint:stringPoint withAttributes:stringAttrib];
What is the calculation that I must do to get fixedHeight say, 32
pixels from top? I know, this is basic but I do not get it.
You could do the calculations as Ricky suggested, or just return YES
from the - (BOOL)isFlipped method, in your NSView subclass. This
will invert the Y origin. Then you can set your origin to 32.0, as
you probably already tried. Whether you want to adjust for the font
ascent and other factors too, is up to you. I'm not sure if the
local origin when drawing strings is taken as (a), (b) or is
respectful of - (BOOL)isFlipped, so you might have to play a little.
Note that theBounds.origin.y can be removed if your bounds will
always have a y-coord of 0.
Don't make assumptions like this in general. The performance and
readability penalty for that extra factor is miniscule.
For further reading, I would suggest http://developer.apple.com/
documentation/Cocoa/Conceptual/DrawViews/index.html. It's a very
good introduction to AppKit drawing in general, including co-ordinate
systems and transforms.
Wade
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden