“打印”部分中几乎没有活动的事实表明,要么没有多少人在使用它,要么使用此 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];
}
我仍然不知道是否有附加 htmlFormatter 和 listHtmlFormatter 的可打印内容(使用这种方法)。例如,不要为 listHtmlFormatter 使用新页面,而是从 htmlFormatter 结束的地方继续打印。