• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: How to calculate diagonal size of text?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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
  • Prev by Date: Re: ftp to Linux machine with utf-8 encoding characters in the filename
  • Next by Date: Re: try/catch/throw/finally example?
  • Previous by thread: Re: How to calculate diagonal size of text?
  • Next by thread: Re: Re: How to calculate diagonal size of text?
  • Index(es):
    • Date
    • Thread