我使用UIActivityViewController打印(以及其他活动)。因此,我传递给它一个UIPrintPageRenderer自定义子类的实例,相关代码如下所示。
本质上,我想打印两个多行属性化字符串,并排,就像两列(最终,我希望一个嵌入另一个,包装,但不要走在我们的前面)。右侧文本视图必须根据其内容为固定大小(它的子类覆盖sizeToFit()以实现这一点)。左侧文本视图应填充剩余宽度。
因此,我使用使用属性化字符串填充的UITextView实例,并将它们各自的.viewPrintFormatter()‘输出作为UIPrintFormatters分配给UIPrintPageRenderer。
这部分有效。两个属性化字符串都打印在页面上。
但是,它们是相互打印的,都在页面的左边边缘。
我试图使用UIEdgeInsets限制它们的打印失败,除非我硬代码值。这似乎是因为我在查询0时得到了printableRect.size.width (0)。
为什么我的UIPrintPageRendere的printableRect总是零宽度?
怎样才能实现两个多行属性字符串的并排打印?
class CustomPrintPageRenderer: UIPrintPageRenderer {
let leftTextView = UITextView(frame: CGRect(x: 0, y: 0, width: 100, height: 100))
let rightTextView = IngredientsTextView(frame: CGRect(x: 0.0, y: 0.0, width: 100.0, height: 100.0))
init(_ thing: Thing) {
super.init()
addThing(thing)
}
func addThing(_ thing: Thing) {
// Do some stuff here to populate the two text views with attributed strings
// ...
// ...
rightTextView.sizeToFit()
let leftPrintFormatter = leftTextView.viewPrintFormatter()
let rightPrintFormatter = rightTextView.viewPrintFormatter()
print(paperRect.size.width)
print(printableRect.size.width)
rightPrintFormatter.perPageContentInsets = UIEdgeInsets(top: formatter.titleFontSize, left: printableRect.size.width - rightTextView.frame.size.width, bottom: 0, right: 0)
leftPrintFormatter.perPageContentInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: rightTextView.frame.size.width)
addPrintFormatter(leftPrintFormatter, startingAtPageAt: numberOfPages)
addPrintFormatter(rightPrintFormatter, startingAtPageAt: numberOfPages)
}
}发布于 2020-03-15 03:29:41
我已经想明白了。paperRect和printableRect属性似乎在init()时不可用(这就是我调用addThing()的地方)。
我必须通过重写其他函数(如drawPrintFormatter()或numberOfPages() )来完成这项工作。
这项工作基本上与预期的一样:
override func drawPrintFormatter(_ printFormatter: UIPrintFormatter, forPageAt pageIndex: Int) {
if printFormatter == rightPrintFormatter {
printFormatter.perPageContentInsets = UIEdgeInsets(top: RecipeFormatter.titlePrintTextSize, left: printableRect.size.width - ingrWidth, bottom: 0, right: 0)
} else if printFormatter == leftPrintFormatter {
printFormatter.perPageContentInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: ingrWidth)
}
super.drawPrintFormatter(printFormatter, forPageAt: pageIndex)
}https://stackoverflow.com/questions/60680240
复制相似问题