有没有人使用多个格式化(试过UIViewPrintFormatter
,UIMarkupTextPrintFormatter
,UISimpleTextPrintFormatter
)与页面渲染(UIPrintPageRenderer
)打印的内容?
我试图用两个UIMarkupTextPrintFormatters
有UIPrintPageRenderer
子类,但我没有得到打印.我正在使用PrintWebView示例代码中的MyPrintPageRenderer
类.
我已经浏览了Apple的文档,但它没有太大帮助,并且没有与描述相关的示例代码.我尝试了几种解决方案,但到目前为止我还没有取得任何成功.
有什么建议?
"打印"部分中的活动非常少,这表明要么没有多少人使用此功能,要么使用此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结束的地方继续打印.