在 iOS 中将一个或多个格式化程序与页面渲染器一起使用
有没有人尝试过使用多个格式化程序(UIViewPrintFormatter
、UIMarkupTextPrintFormatter
、UISimpleTextPrintFormatter
)和页面渲染器(UIPrintPageRenderer
)进行打印内容?
我试图将两个 UIMarkupTextPrintFormatters
与 UIPrintPageRenderer
子类一起使用,但我无法获得打印。我正在使用 中的 MyPrintPageRenderer
类PrintWebView示例代码。
我已经浏览了苹果的 文档,但它不是很有帮助,并且没有与描述相关的示例代码。我尝试了几种解决方案,但到目前为止我还没有取得任何成功。
有什么建议吗?
Has anyone tried using multiple formatters (UIViewPrintFormatter
, UIMarkupTextPrintFormatter
, UISimpleTextPrintFormatter
) with a page renderer (UIPrintPageRenderer
) to print the content?
I'm trying to use two UIMarkupTextPrintFormatters
with a UIPrintPageRenderer
subclass, but i'm failing to get the print. I'm using MyPrintPageRenderer
class from PrintWebView sample code.
I've gone through the Apple's documentation but it isn't very helpful, and there is no sample code associated with the description. I've tried a couple of solutions but so far I haven't had any success.
Any suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
事实上,“打印”部分的活动非常少,这表明要么没有多少人使用此 API,要么使用此 API 的人没有访问此社区,要么人们只是出于某种原因忽略了该问题。
无论如何,我能够解决我的问题。我之前使用的是
setPrintFormatters:
方法,但它不起作用。我不知道为什么。因此,我开始尝试使用addPrintFormatter:startingAtPageAtIndex:
方法。这就是我解决问题的方法:在
MyPrintPageRenderer
中,我使用以下代码来更新/更正 startPage 属性,以便每个格式化程序使用一个新页面:我仍然不知道是否有无论如何都要附加 htmlFormatter 和 listHtmlFormatter 的可打印内容(使用这种方法)。例如,不要为 listHtmlFormatter 使用新页面,而是从 htmlFormatter 结束的位置继续打印。
The fact that there is very little activity in the "Printing" section suggests that either not many people are using this, or people using this API are not visiting this community, or people are just ignoring the question for some reason.
Anyways, i was able to solve my problem. I was using
setPrintFormatters:
method earlier which wasn't/isn't working. I don't know why. So, i started experimenting withaddPrintFormatter:startingAtPageAtIndex:
method instead. And here's how i solved my problem:In the
MyPrintPageRenderer
, i used following code to update/correct the startPage property so that a new page is used for each formatter:I still don't know if there is anyway to APPEND the printable content of both htmlFormatter and listHtmlFormatter (using this approach). e.g. rather than using a new page for listHtmlFormatter, continue printing from where htmlFormatter ended.
我在这里添加这个额外信息答案是因为我花了 2 天的时间研究这个和 Mustafa 的答案救了我。希望所有这些都集中在一个地方,可以为其他人节省大量浪费的时间。
我试图弄清楚为什么我无法让自定义的 UIPrintPageRenderer 迭代我需要打印的多个 UIWebViews 中的 viewPrintFormatter 数组在一项工作中,并让它正确计算 pageCount,正如这些 Apple 文档建议的那样:PrintWebView
关键是通过此方法添加它们:
和 <强>不这个:
Apple 文档建议 UIPrintPageRenderer 从数组中的打印格式化程序获取页数,只要正确的指标在打印格式化程序上设置,如 Mustafa 上面所示。 但是仅当您通过
addPrintFormatter:startingAtPageAtIndex:
添加它们时才有效。如果您将它们添加为数组,则 printFormatters(无论您使用什么指标)设置)将返回页数 0!
对于使用此方法的人来说,一个附加说明是将
viewPrintFormatter.startPage
的更新放在dispatch_async
块中,否则您将收到异常:I'm adding this extra info answer here because I spent 2 days dk'n around with this and Mustafa's answer saved me. Hopefully all this in one place will save others a lot of wasted time.
I was trying to figure out why I was unable get my custom
UIPrintPageRenderer
to iterate over an array ofviewPrintFormatter
from severalUIWebViews
that I need to print in ONE job, and have it properly calculate pageCount as these Apple docs suggest it should: PrintWebViewThe key was to add them via this method:
and NOT this one:
The Apple docs suggest that the
UIPrintPageRenderer
gets page count from the print formatters in the array as long as the proper metrics are set on the print formatters, as Mustafa shows above. But it only works if you add them viaaddPrintFormatter:startingAtPageAtIndex:
If you add them as an array, the printFormatters (regardless of what metrics you set on them) will return a page count of 0!
One additional note for people using this approach, put the update to
viewPrintFormatter.startPage
in adispatch_async
block, else you'll get an exception:要添加@Mustafa的答案,
startPage 需要声明为:
如果要在dispatch_async块内使用它
To add to @Mustafa's answer,
startPage needs to be declared as:
if it's to be used inside the dispatch_async block