如何打印整个NSCollectionView

时间:2017-02-03 09:55:50

标签: swift cocoa pdf printing nscollectionview

我试图打印整个NSCollectionView。它只打印显示的项目,而且它不能正确呈现NSCollectionViewItem

enter image description here

使用第一响应者通过绑定填充打印。方法:

 @IBAction func printEquations(_ sender: Any) {

    let op = NSPrintOperation(view: collectionView)
    op.canSpawnSeparateThread = true

    let pi = op.printInfo

    pi.horizontalPagination = NSPrintingPaginationMode.fitPagination
    pi.verticalPagination = NSPrintingPaginationMode.fitPagination
    pi.isHorizontallyCentered = true
    pi.isVerticallyCentered = true
    pi.isSelectionOnly = false

    pi.orientation = NSPaperOrientation.portrait
    pi.leftMargin = 10
    pi.rightMargin = 10

    op.run()


}

NSCollectionView子类:

class EQCollectionView: NSCollectionView {

var printRect: NSRect?

override func draw(_ dirtyRect: NSRect) {

    if (NSGraphicsContext.currentContextDrawingToScreen()) {

        self.layer!.borderWidth = 0
        self.layer!.borderColor = nil
        self.backgroundColors = []
        super.draw(dirtyRect)

    } else {

        self.wantsLayer = true


        let printWidth = Double(calculatePrintWidth())
        let printHeight = Double(calculatePrintHeight())

        printRect = NSRect(x: 0, y: 0, width: printWidth, height: printHeight)

        super.draw(printRect!)
    }
}



func calculatePrintHeight() -> Float {

    let pi = NSPrintOperation.current()!.printInfo

    let paperSize = pi.paperSize

    let pageHeight = Float(paperSize.height - pi.topMargin - pi.bottomMargin)

    let scale = Float(pi.dictionary().object(forKey: NSPrintScalingFactor) as! CGFloat)

    return pageHeight / scale
}

func calculatePrintWidth() -> Float {

    let pi = NSPrintOperation.current()!.printInfo

    let paperSize = pi.paperSize

    let pageWidth = Float(paperSize.width - pi.leftMargin - pi.rightMargin)

    let scale = Float(pi.dictionary().object(forKey: NSPrintScalingFactor) as! CGFloat)

    return pageWidth / scale
}

override func knowsPageRange(_ range: NSRangePointer) -> Bool {
    range.pointee.location = 1
    range.pointee.length = 1
    return true
}


override func rectForPage(_ page: Int) -> NSRect {

    let bounds = self.bounds
    let pageHeight = calculatePrintHeight()

    let rect = NSMakeRect( NSMinX(bounds), NSMinY(bounds), frame.width, CGFloat(pageHeight));


    return rect;
}

}

如何使用所有可用内容打印NSCollectionView?

0 个答案:

没有答案