Re: Positioning a string
Re: Positioning a string
- Subject: Re: Positioning a string
- From: Andrew Thompson <email@hidden>
- Date: Mon, 30 Jan 2006 15:20:44 -0800
On Jan 30, 2006, at 2:00 PM, Julio Cesar Silva dos Santos wrote:
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 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.
Thanks for any help,
To reverse the coordinate system, just subtract the point from the
highest value, so
point.y = topOfView-point.y
where topOfView is the height of the view minus one. (Minus one
because the view coordinate system starts at zero).
The string is drawn from the bottom left corner, so, the point is 32
pixels from the top plus the hight of the string.
then you have to reverse it for the coordinate system, so:
point.y = 32 + stringSize.height;
point.y =topOfView - point.y;
Or to put it into your program:
NSSize outerSize = [self bounds].size;
NSSize stringSize = [theString sizeWithAttributes:stringAttrib];
NSPoint stringPoint;
stringPoint.x = fixedWidth;
stringPoint.y = fixedWidth+stringSize.height;
stringPoint.y = (outSize-1) - stringPoint.y ;
[theString drawAtPoint:stringPoint withAttributes:stringAttrib];
Hope that helps. A lot of times for strange geometric problems it
helps me to draw a picture on paper to figure it out.
_______________________________________________
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