创建自动调整大小的打印输出 [英] Creating an autoresizing printout

查看:110
本文介绍了创建自动调整大小的打印输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序需要打印一些内容.布局应具有一定的动态性(有时特定字段可能包含更多数据,这可能要求将它们用文字包装或类似方法包装),但打印输出不应超过单个页面.如果数据太多,我想稍微减小字体大小,然后再试一次,然后重复一遍,直到所有内容都适合在单个页面上为止(不会再有如此的数据表明字体太小了) ).

我需要在同一页面上同时打印文本和图像.

布局应类似于以下内容:

+------------------------------------------------------------------+
| +------+                                               +-------+ |
| | seal |                   text field 1                | photo | |
| |      |                   text field 2                |       | |
| +------+                                               +-------+ |
| ---------------------------------------------------------------- |
|  text header 1:                 text data                        |
|  text header 2:                 longer text data                 |
|  text header 3 with long title: very long text data which does   |
|                                 not fit on a single line         |
| ---------------------------------------------------------------- |
|  text header 4:                 text data                        |
.                                                                  .
.     (...)                          (...)                         .
.                                                                  .
|                                                                  |
+------------------------------------------------------------------+

text field 1的数据略有变化,但并不重要. text field 2的数据可能会很短,但也可能会非常长,并且应该适合两个图像元素之间的位置,必要时可以进行换行.两个视场都应该(大约)垂直居中于两个图像之间.

页面其余部分的数据字段之间用竖线分组,但是所有数据字段应从页面左侧开始以相同的偏移量开始,该偏移量由最长的页眉的长度定义.

我已经在我的XIB文件中使用独立的NSView(独立于窗口的一部分)来部分管理此问题.我创建了NSTextField elemnts来保存标头和数据.对齐要求被定义为对NSTextField元素的约束,并且如果所有内容都适合(即,不需要自动换行,并且不需要减小字体大小),则它看起来应该像应该的样子./p>

但是,仍然存在一些问题:

  • 我无法弄清楚如何使NSTextField换行,但是要根据需要适应所有尺寸,使其垂直方向保持很小.看起来我要么需要使其高度超过一行(这将导致行之间出现不必要的空白空间),要么自动换行会使第二行模糊.
  • 打印API希望我在知道页面上的可打印区域之前传递NSView.因此,我没有选择调整NSView大小的选项,因此它无法最佳地适合页面.当前,这导致页面的侧边距过大或上/下边距过大,具体取决于所使用的打印机及其设置.

我要这样做正确吗?如果是这样,我应该如何解决以上两个问题?如果没有,我应该怎么做?

我确实看过Core Printing API,但这似乎有点太低了.

谢谢

解决方案

如果将Behavior(行为)设置为None(无)或Selectable(可选),则NSTextField将自动换行并足够高.

要调整打印视图的大小,请创建NSView的子类并实现KnowsPageRange:和rectForPage:.在KnowsPageRange中:计算页数(1),然后返回YES.在rectForPage中:设置高度和宽度约束,以便视图与页面大小相同,调用[self layoutSubtreeIfNeeded]并返回self.bounds. [[NSPrintOperation currentOperation] printInfo]将为您提供printinfo.

My application needs to print something. The layout should be somewhat dynamic (sometimes particular fields may contain more data, which could require them being word-wrapped or some such), but the printout should never be more than a single page. If there's too much data, I want to reduce the font size slightly and try again, and repeat that until everything fits on a single page (there will never be so many data that the font will be unreadably small).

I need to print both text and images on the same page.

The layout should look something like the following:

+------------------------------------------------------------------+
| +------+                                               +-------+ |
| | seal |                   text field 1                | photo | |
| |      |                   text field 2                |       | |
| +------+                                               +-------+ |
| ---------------------------------------------------------------- |
|  text header 1:                 text data                        |
|  text header 2:                 longer text data                 |
|  text header 3 with long title: very long text data which does   |
|                                 not fit on a single line         |
| ---------------------------------------------------------------- |
|  text header 4:                 text data                        |
.                                                                  .
.     (...)                          (...)                         .
.                                                                  .
|                                                                  |
+------------------------------------------------------------------+

The data for text field 1 is slightly variable, but not to the extent that it matters. The data for text field 2 can be fairly short, but can be extremely long too, and should fit between the two image elements, wordwrapping if necessary. Both fields should be (approximately) centered, vertically, between the two images.

The data fields on the rest of the page is grouped with vertical lines in between, but all data fields should start at the same offset from the left of the page, which is defined by the length of the longest header.

I've managed this partially using a freestanding NSView (freestanding, as in, not part of a window) in my XIB file. I've created NSTextField elemnts to hold the headers and the data. The alignment requirements are defined as constraints on the NSTextField elements, and if everything fits (i.e., no word wrapping is required, and the font size doesn't need to be reduced), it looks vaguely like the way it should.

However, there are a few issues remaining:

  • I can't figure out how to make an NSTextField wrap its text but stay as small, vertically, as needed to fit everything. It looks like I either need to make it more than one line high (which would result in unwanted empty space between lines), or the word wrap will obscure the second line.
  • The printing API wants me to pass an NSView before I know what the printable area on the page will be. As such, I don't see an option for me to resize the NSView so it fits optimally on the page. Currently, that results in a page which has overly large side margins or overly large top/bottom margins, depending on the printer used and its settings.

Am I going about this the right way? If so, how should I fix the above two issues? If not, what should I be doing instead?

I did have a look at the Core Printing API, but that seems a bit too low level.

Thanks,

解决方案

A NSTextField will wrap and will be just high enough if you set Behaviour to None or Selectable.

To resize the print view, you create a subclass of NSView and implement knowsPageRange: and rectForPage:. In knowsPageRange: you calculate the number of pages (1) and return YES. In rectForPage: you set height and width constraints so the view will be the same size as the page, call [self layoutSubtreeIfNeeded] and return self.bounds. [[NSPrintOperation currentOperation] printInfo] will give you the printinfo.

这篇关于创建自动调整大小的打印输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆