views:

393

answers:

1

I'm using an NSTextField to display lines of text that will be printed on a printer with a variable paper size (the printer prints to a spool, which is cut to the appropriate length when the job is done).

I can generate the text no problem, but I need to know how big to make the NSTextField (vertically) so that it exactly contains all of the lines of text. Is there an easy way of going about this?

The text is prepared as an NSAttributedString, and the lines are all of a fixed width (no wrapping), if that helps.

+3  A: 

There is a great category on NSString/NSAttributedString by Jerry Krinock which allows you to calculate the height of text based on its width and vice versa:

http://www.sheepsystems.com/sourceCode/sourceStringGeometrics.html

I've used this often and it works very well indeed.

Rob Keniger
That works perfectly. Thanks!
e.James