在iOS中使用一个或多个格式器与页面呈现器

有没有人试图使用多个格式( UIViewPrintFormatterUIMarkupTextPrintFormatterUISimpleTextPrintFormatter )与页面渲染器( UIPrintPageRenderer )来打印内容?

我试图使用两个UIMarkupTextPrintFormattersUIPrintPageRenderer子类,但我无法获得打印。 我使用PrintWebView示例代码中的MyPrintPageRenderer类。

我已经通过了苹果的文档,但它不是很有帮助,没有与描述相关的示例代码。 我已经尝试了几个解决scheme,但到目前为止我还没有取得任何成功。

有什么build议么?

“打印”部分的活动很less,这表明没有多less人使用这个API,或者使用这个API的人没有访问这个社区,或者人们由于某种原因而忽略了这个问题。

无论如何,我能解决我的问题。 我以前使用setPrintFormatters:方法是不是/不工作。 我不知道为什么。 所以,我开始尝试使用addPrintFormatter:startingAtPageAtIndex:方法。 以下是我如何解决我的问题:

 // To draw the content of each page, a UIMarkupTextPrintFormatter is used. NSString *htmlString = [self prepareWebViewHTML]; UIMarkupTextPrintFormatter *htmlFormatter = [[UIMarkupTextPrintFormatter alloc] initWithMarkupText:htmlString]; NSString *listHtmlString = [self prepareWebViewListHTMLWithCSS]; UIMarkupTextPrintFormatter *listHtmlFormatter = [[UIMarkupTextPrintFormatter alloc] initWithMarkupText:listHtmlString]; // I think this should work, but it doesn't! The result is an empty page with just the header and footer text. // [myRenderer setPrintFormatters:[NSArray arrayWithObjects:htmlFormatter, listHtmlFormatter, nil]]; // Alternatively, i've used addPrintFormatters here, and they just work! // Note: See MyPrintPageRenderer's numberOfPages method implementation for relavent details. // The important point to note there is that the startPage property is updated/corrected. [myRenderer addPrintFormatter:htmlFormatter startingAtPageAtIndex:0]; [myRenderer addPrintFormatter:listHtmlFormatter startingAtPageAtIndex:1]; 

MyPrintPageRenderer ,我使用下面的代码来更新/更正startPage属性,以便每个格式化程序使用一个新的页面:

 - (NSInteger)numberOfPages { // TODO: Perform header footer calculations // . . . NSUInteger startPage = 0; for (id f in self.printFormatters) { UIPrintFormatter *myFormatter = (UIPrintFormatter *)f; // Top inset is only used if we want a different inset for the first page and we don't. // The bottom inset is never used by a viewFormatter. myFormatter.contentInsets = UIEdgeInsetsMake(0, leftInset, 0, rightInset); // Just to be sure, never allow the content to go past our minimum margins for the content area. myFormatter.maximumContentWidth = self.paperRect.size.width - 2*MIN_MARGIN; myFormatter.maximumContentHeight = self.paperRect.size.height - 2*MIN_MARGIN; myFormatter.startPage = startPage; startPage = myFormatter.startPage + myFormatter.pageCount; } // Let the superclass calculate the total number of pages return [super numberOfPages]; } 

我仍然不知道是否有APPEND HTMLFormatter和listHtmlFormatter的可打印内容(使用这种方法)。 例如,而不是使用listHtmlFormatter的新页面,继续从htmlFormatter结束处打印。

我在这里添加这个额外的信息答案 ,因为我花了2天dk'n在这附近, 穆斯塔法的答案救了我。 希望在一个地方这一切都会为他人节省大量的时间。

我试图找出为什么我无法让我的自定义UIPrintPageRenderer从一个UIWebViews遍历一个viewPrintFormatter的数组,我需要在一个工作中打印,并正确计算pageCount,因为这些Apple文档build议它应该: PrintWebView

关键是要通过这种方法添加它们:

 -(void)addPrintFormatter:(UIPrintFormatter *)formatter startingAtPageAtIndex:(NSInteger)pageIndex 

不是这个:

 @property(nonatomic, copy) NSArray <UIPrintFormatter *> *printFormatters 

Apple文档build议, UIPrintPageRenderer在打印格式化程序中设置了正确的度量标准, UIPrintPageRenderer就可以从数组中的打印格式化程序中获取页面计数,如上面的Mustafa所示。 但是 ,只有通过addPrintFormatter:startingAtPageAtIndex:添加它们addPrintFormatter:startingAtPageAtIndex:

如果将它们作为数组添加,则printFormatters(不pipe您在其上设置了什么指标)将返回0的页数!

对于使用这种方法的人来说, 另一个注意事项是把更新放在dispatch_async块的viewPrintFormatter.startPage中,否则你会得到一个exception:

 dispatch_async(dispatch_get_main_queue(), ^{ myFormatter.startPage = startPage; startPage = myFormatter.startPage + myFormatter.pageCount; }); 

要添加到@ Mustafa的答案, startPage需要声明为:

 __block NSUInteger startPage 

如果要在dispatch_async块内使用