gpt4 book ai didi

ios - 在 iOS 中将一个或多个格式化程序与页面呈现器一起使用

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:20:58 24 4
gpt4 key购买 nike

有没有人尝试过将多个格式化程序(UIViewPrintFormatterUIMarkupTextPrintFormatterUISimpleTextPrintFormatter)与页面渲染器(UIPrintPageRenderer)一起使用) 来打印内容?

我正在尝试将两个 UIMarkupTextPrintFormattersUIPrintPageRenderer 子类一起使用,但我无法获得打印。我正在使用 PrintWebView 中的 MyPrintPageRenderer 类示例代码。

我已经查看了 Apple 的 documentation但它不是很有帮助,并且没有与描述相关的示例代码。我尝试了几种解决方案,但到目前为止我还没有取得任何成功。

有什么建议吗?

最佳答案

“打印”部分的事件很少,这表明要么没有多少人在使用这个,要么使用这个 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 结束的地方继续打印。

关于ios - 在 iOS 中将一个或多个格式化程序与页面呈现器一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5946209/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com