Re: How to calculate diagonal size of text?
Re: How to calculate diagonal size of text?
- Subject: Re: How to calculate diagonal size of text?
- From: Wagner Truppel <email@hidden>
- Date: Sat, 26 Aug 2006 15:42:58 -0700
Keith,
contact me directly and I'll send you detailed information on how to
compute the appropriate dimensions. I'd rather not post a long
derivation nor send an attachment to the list (I have the derivation
written in a pdf file). The bottom-line is this, however:
1) First compute some needed variables, from the width W and height H
of your view's bounds rectangle:
d = sqrt(H*H + W*W) (the length of the bounds rectangle diagonal)
a = arctan(H/W) (the angle to rotate the string rectangle by)
2) Given the actual string to be drawn and the font to draw it with,
use the appropriate Cocoa call to determine the length (in pixels)
the string will occupy when drawn horizontally. Call that length w.
If w comes out larger than the view rectangle's diagonal length d,
choose a smaller font.
3) Compute the string rectangle's height h = (H/W) (d - w). If h is
larger than the height of the tallest character of your string, you
need once again to reduce your font size.
4) At this point, you've determined the width (w) and height (h) of
the rectangle inside of which the string will be drawn. You can use
these values to center the string both vertically and horizontally
inside this rectangle, using the appropriate Cocoa calls. I think the
easiest approach is to have a custom subclass of NSView whose only
duty is to draw a string centered in its frame rectangle.
5) Compute the horizontal position of the anchor point around which
the string rectangle should be rotated: x = d(d - w)/(2W). The
vertical position of the anchor point is zero, assuming the vertical
axis is oriented upwards.
6) Finally, position the custom NSView just described such that its
lower left vertex lies at the position determined by the anchor
point, then rotate it counterclockwise by an angle a. This is a
combination of a rotation and a translation, both of which you can
define using NSAffineTransform objects.
I should point out that the expressions above assume W >= H, that is,
that your view rectangle is longer than taller, or a square. If
that's not the case, the equations need to be changed a little.
Wagner
============
How many astronomers does it take to change a light bulb?
None, astronomers prefer the dark.
_______________________________________________
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