2

我正在尝试使用将字符串发送到打印机UIMarkupTextPrintFormatter,当我不更改页面方向时一切都很好 - 默认情况下设置为纵向。

纵向打印预览

但是,当我将页面方向设置为横向时,一个空白页面会附加到呈现的输出中,就好像页边距超出了第一页一样。

横向打印预览

这是我的打印代码:

let printController = UIPrintInteractionController.sharedPrintController()

let printInfo = UIPrintInfo(dictionary: nil)
printInfo.jobName = "Print Job"
printInfo.orientation = .Landscape
printController.printInfo = printInfo

let formatter = UIMarkupTextPrintFormatter(markupText: "Welcome")
formatter.contentInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
printController.printFormatter = formatter

printController.presentAnimated(true, completionHandler: nil)

另外需要注意的是,当页面设置为横向时,Xcode 会记录一些与自动布局相关的错误。

the behavior of the UICollectionViewFlowLayout is not defined because:
the item height must be less than the height of the UICollectionView minus the section insets top and bottom values, minus the content insets top and bottom values.
The relevant UICollectionViewFlowLayout instance is <UICollectionViewFlowLayout: 0x7fa066097850>, and it is attached to <UICollectionView: 0x7fa06687f800; frame = (0 0; 375 313.5); clipsToBounds = YES; opaque = NO; autoresize = W+H; gestureRecognizers = <NSArray: 0x7fa066095e90>; layer = <CALayer: 0x7fa066098b30>; contentOffset: {0, 0}; contentSize: {666, 313.5}> collection view layout: <UICollectionViewFlowLayout: 0x7fa066097850>.
Make a symbolic breakpoint at UICollectionViewFlowLayoutBreakForInvalidSizes to catch this in the debugger.
the behavior of the UICollectionViewFlowLayout is not defined because:
the item height must be less than the height of the UICollectionView minus the section insets top and bottom values, minus the content insets top and bottom values.
The relevant UICollectionViewFlowLayout instance is <UICollectionViewFlowLayout: 0x7fa066097850>, and it is attached to <UICollectionView: 0x7fa06687f800; frame = (0 0; 375 313.5); clipsToBounds = YES; opaque = NO; autoresize = W+H; gestureRecognizers = <NSArray: 0x7fa066095e90>; layer = <CALayer: 0x7fa066098b30>; contentOffset: {0, 0}; contentSize: {666, 313.5}> collection view layout: <UICollectionViewFlowLayout: 0x7fa066097850>.
Make a symbolic breakpoint at UICollectionViewFlowLayoutBreakForInvalidSizes to catch this in the debugger.
Unbalanced calls to begin/end appearance transitions for <UIViewController: 0x7fa066085830>.
the behavior of the UICollectionViewFlowLayout is not defined because:
the item height must be less than the height of the UICollectionView minus the section insets top and bottom values, minus the content insets top and bottom values.
The relevant UICollectionViewFlowLayout instance is <UICollectionViewFlowLayout: 0x7fa063c55130>, and it is attached to <UICollectionView: 0x7fa064043000; frame = (0 0; 375 313.5); clipsToBounds = YES; opaque = NO; autoresize = W+H; gestureRecognizers = <NSArray: 0x7fa063c4a720>; layer = <CALayer: 0x7fa063c26e60>; contentOffset: {0, 0}; contentSize: {666, 313.5}> collection view layout: <UICollectionViewFlowLayout: 0x7fa063c55130>.
Make a symbolic breakpoint at UICollectionViewFlowLayoutBreakForInvalidSizes to catch this in the debugger.
the behavior of the UICollectionViewFlowLayout is not defined because:
the item height must be less than the height of the UICollectionView minus the section insets top and bottom values, minus the content insets top and bottom values.
The relevant UICollectionViewFlowLayout instance is <UICollectionViewFlowLayout: 0x7fa063c55130>, and it is attached to <UICollectionView: 0x7fa064043000; frame = (0 0; 375 313.5); clipsToBounds = YES; opaque = NO; autoresize = W+H; gestureRecognizers = <NSArray: 0x7fa063c4a720>; layer = <CALayer: 0x7fa063c26e60>; contentOffset: {0, 0}; contentSize: {666, 313.5}> collection view layout: <UICollectionViewFlowLayout: 0x7fa063c55130>.
Make a symbolic breakpoint at UICollectionViewFlowLayoutBreakForInvalidSizes to catch this in the debugger.

我试图只打印第一页,但找不到任何关于设置页面范围的信息,可以设置UIMarkupTextPrintFormatter一个startPage变量,但设置它并不走运,即使我将它设置为第二页,它也会呈现为t 设置为任何东西。

我可以通过删除空白页或仅将打印范围设置为第 1 页来解决此问题,但到目前为止我无法完成其中任何一个。有任何想法吗?

4

1 回答 1

1

看来我不能通过直接设置printFormatterfor来解决这个问题UIPrintInteractionController,而是创建一个UIPrintPageRenderer然后将我的格式化程序附加到渲染器,然后再将其设置为UIPrintInteractionController.

这是解决方案:

// printController.printFormatter = formatter // cancelled
let printRenderer = UIPrintPageRenderer()
printRenderer.addPrintFormatter(formatter, startingAtPageAtIndex: 0)
printController.printPageRenderer = printRenderer
于 2015-11-26T09:42:37.133 回答