如何调整 NSTextField 的大小以适合它所包含的文本?
我使用 NSTextField
来显示将在具有可变纸张尺寸的打印机上打印的文本行(打印机打印到线轴,当作业完成时将线轴切割为适当的长度)。
我可以毫无问题地生成文本,但我需要知道 NSTextField
(垂直)有多大,以便它准确包含所有文本行。有没有一种简单的方法可以解决这个问题?
文本准备为 NSAttributedString ,并且所有行都具有固定宽度(无换行)(如果有帮助的话)。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Jerry Krinock 的 NSString/NSAttributedString 上有一个很棒的类别,它允许您根据文本的宽度计算文本的高度,反之亦然:
http://www.sheepsystems.com/sourceCode/sourceStringGeometrics.html
我经常使用这个,它确实工作得很好。
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.