首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何一次AirPrint多个打印格式化程序?

如何一次AirPrint多个打印格式化程序?
EN

Stack Overflow用户
提问于 2016-12-06 16:03:14
回答 1查看 859关注 0票数 16

我的最终目标是创建多个UIMarkupTextPrintFormatter,并在一个打印作业中打印它们。

例如,第一个格式化程序包含填充第一页和少量第二页的文本。尽管仍有空间用于打印第二个格式化程序,但我希望在第三页上打印第二个格式化程序。因此,基本上,在打印另一个格式化程序之前,请开始新的页面。

我不知道一个格式化程序在编译时会填满多少页,因为它依赖于用户输入。可能是1个、2个或更多。

我听说我需要使用UIPrintPageRenderer,所以我阅读了它的文档,并尝试了一下:

代码语言:javascript
复制
// I'm using webviews here because I don't want to write 2 pages worth of markup...
// it should be the same anyway. The two webviews display different wikipedia articles
// Also, don't judge my variable names. I'm lazy and this is just test code...
let formatter = webView.viewPrintFormatter()
let formatter1 = webView2.viewPrintFormatter()

formatter.contentInsets = UIEdgeInsets(top: 72, left: 72, bottom: 72, right: 72)
formatter1.contentInsets = UIEdgeInsets(top: 72, left: 72, bottom: 72, right: 72)

let renderer = UIPrintPageRenderer()
renderer.addPrintFormatter(formatter, startingAtPageAt: 0)
renderer.addPrintFormatter(formatter1, startingAtPageAt: formatter.pageCount)

printController.printPageRenderer = renderer
printController.present(animated: true, completionHandler: nil)

为了添加第二个打印格式化程序,我使用formatter.pageCount作为第二个参数。我希望它能在第一个格式化程序的最后一页之后添加第二个格式化程序。但它没有。它只打印了第二个格式化程序中的内容。

我打印了formatter.pageCount,发现它的值是0。

我完全糊涂了。如何获取一个格式化程序将填满多少页?

我还尝试实现了我自己的渲染器:

代码语言:javascript
复制
class MyRenderer: UIPrintPageRenderer {
    override func drawPrintFormatter(_ printFormatter: UIPrintFormatter, forPageAt pageIndex: Int) {
        printFormatter.draw(in: self.printableRect, forPageAt: pageIndex)
    }
}

但当打印交互控制器显示以下内容时,它就会崩溃:

尝试从主线程或web线程以外的线程获取web锁。这可能是从辅助线程调用UIKit的结果。现在崩溃了。

我在代码中做错了什么?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-09-26 13:15:53

根据文档,UIPrintFormatter在计算打印格式的页数之前需要所有的信息。

UIPrintFormatter发布一个界面,该界面允许您指定打印作业的起始页和打印内容周围的页边距;给定该信息加上内容,打印格式化程序将计算打印作业的页数。

因为UIPrintPageRenderer可以使用UIPrintFormatter,所以它实际上可以获得与格式化程序相关联的页面数量,您所要做的就是覆盖numberOfPages,以便在每个页面上设置不同的格式化程序。

下面是来自Apple的示例代码。

它在这里覆盖了numberOfPages:

代码语言:javascript
复制
- (NSInteger)numberOfPages {
    self.printFormatters = nil;
    [self setupPrintFormatters];
    return [super numberOfPages];
}

代码语言:javascript
复制
  /*
     Iterate through the recipes setting each of their html representations into 
     a UIMarkupTextPrintFormatter and add that formatter to the printing job.
     */
    - (void)setupPrintFormatters {
        NSInteger page = 0;
        CGFloat previousFormatterMaxY = CGRectGetMinY(self.contentArea);

        for (Recipe *recipe in recipes) {
            NSString *html = recipe.htmlRepresentation;

            UIMarkupTextPrintFormatter *formatter = [[UIMarkupTextPrintFormatter alloc] initWithMarkupText:html];
            [formatterToRecipeMap setObject:recipe forKey:formatter];

            // Make room for the recipe info
            UIEdgeInsets contentInsets = UIEdgeInsetsZero;
            contentInsets.top = previousFormatterMaxY + self.recipeInfoHeight;
            if (contentInsets.top > CGRectGetMaxY(self.contentArea)) {
                // Move to the next page
                page++;
                contentInsets.top = CGRectGetMinY(self.contentArea) + self.recipeInfoHeight;
            }
            formatter.contentInsets = contentInsets;

            // Add the formatter to the renderer at the specified page
            [self addPrintFormatter:formatter startingAtPageAtIndex:page];

            page = formatter.startPage + formatter.pageCount - 1;
            previousFormatterMaxY = CGRectGetMaxY([formatter rectForPageAtIndex:page]);

        }
    }

简而言之,它是根据内容区域、内容插入计算页码,然后通过调用API将格式化程序附加到页面:

代码语言:javascript
复制
 [self addPrintFormatter:formatter startingAtPageAtIndex:page];

这样,渲染器就可以获得有关每个页面的格式化程序的信息。你可以找到完整的代码here

希望这能有所帮助。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40990514

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档