我有一个打印 NSView 的 Swift Cocoa 程序。 它在蒙特雷工作得很好,但在索诺玛确实显示了打印面板。 我找不到问题所在。 以下是 4 个错误:
无法将(genericPrinterImage)出口从(PMPrinterSelectionController)连接到(NSImageView):缺少设置器或实例变量
无法将(otherPrintersLabel)出口从(PMPrinterSelectionController)连接到(NSTextField):缺少设置器或实例变量
无法将(localPrintersLabel)出口从(PMPrinterSelectionController)连接到(NSTextField):缺少设置器或实例变量
无法将(genericPrinterImage)出口从(PMPrinterSelectionController)连接到(NSImageView):缺少设置器或实例变量
func createPrintOperation() {
let printOpts: [NSPrintInfo.AttributeKey: Any] = [
.headerAndFooter: false,
.orientation: NSPrintInfo.PaperOrientation.portrait
]
let printInfo = NSPrintInfo(dictionary: printOpts)
printInfo.leftMargin = 0
printInfo.rightMargin = 0
printInfo.topMargin = 0
printInfo.bottomMargin = 0
printInfo.horizontalPagination = .fit
printInfo.verticalPagination = .automatic
printInfo.isHorizontallyCentered = true
printInfo.isVerticallyCentered = true
printInfo.scalingFactor = 1.0
printInfo.paperSize = NSMakeSize(612, 792) // Letter size
// Create a print operation with the view you want to print , myPrintView is a NSView
let printOperation = NSPrintOperation(view: myPrintView, printInfo: printInfo)
// Configure the print panel
printOperation.printPanel.options.insert(NSPrintPanel.Options.showsPaperSize)
printOperation.printPanel.options.insert(NSPrintPanel.Options.showsOrientation)
// Set the job title for the print operation
let jobTitle = fact.nom_complet_f.replacingOccurrences(of: " ", with: "")
printOperation.jobTitle = jobTitle
// Run the print operation
printOperation.run()
}
感谢 ChatGPT,该代码可以正常工作。
func createPrintOperation() {
let printOpts: [NSPrintInfo.AttributeKey: Any] = [.headerAndFooter: false,.orientation : 1] // set
let printInfo = NSPrintInfo(dictionary: printOpts)
printInfo.leftMargin = 0
printInfo.rightMargin = 0
printInfo.topMargin = 0
printInfo.bottomMargin = 0
printInfo.horizontalPagination = .fit
printInfo.verticalPagination = .automatic
printInfo.isHorizontallyCentered = true
printInfo.isVerticallyCentered = true
printInfo.scalingFactor = 1.0
printInfo.paperSize = NSMakeSize(612, 792) // letter
printInfo.orientation = .portrait
let printOperation = NSPrintOperation(view: PrintView, printInfo: printInfo)
// Attempt to show paper size and orientation options
printOperation.printPanel.options.insert(.showsPaperSize)
printOperation.printPanel.options.insert(.showsOrientation)
// Set the job title
let jobTitle = fact.nom_complet_f.replacingOccurrences(of: " ", with: "")
printOperation.jobTitle = jobTitle
// Ensure the print operation runs on the main thread
DispatchQueue.main.async {
printOperation.run()
}
`