How to set up printing in cocoa, fast?

I made a print function for the custom NSView NSPopover by assigning the following button action for this NSView in the mainController:

@IBOutlet var plasmidMapIBOutlet: PlasmidMapView! @IBAction func actionPrintfMap(sender: AnyObject) { plasmidMapIBOutlet.print(sender) } 

It works, but there is no Paper Size and Orientation option in the print window, see screenshot below. enter image description here

  • What to do to get these options in the print window?
  • And how to make the NSView fitting printable? Now it does not fit.

I found out some points, but not completely. Thus, I can configure printing with the following code

  @IBAction func actionPrintMap(sender: AnyObject) { let printInfo = NSPrintInfo.sharedPrintInfo() let operation: NSPrintOperation = NSPrintOperation(view: plasmidMapIBOutlet, printInfo: printInfo) operation.printPanel.options = NSPrintPanelOptions.ShowsPaperSize operation.printPanel.options = NSPrintPanelOptions.ShowsOrientation operation.runOperation() //plasmidMapIBOutlet.print(sender) } 

But I still have a problem. From the code above, I can only get the orientation (the latter, ShowsOrientation), but not both PaperSize and Orientation. How can I manage both ShowsPaperSize and ShowsOrientation?

+5
source share
2 answers

Finally, I found an answer that is easy to write, but this is not entirely obvious from the Apple documentation.

  operation.printPanel.options.insert(NSPrintPanelOptions.showsPaperSize) operation.printPanel.options.insert(NSPrintPanelOptions.showsOrientation) 
+3
source

The problem with the originally published code is that options are assigned twice, so the first value assigned, ShowsPaperSize , is overwritten by the value of ShowsOrientation . Therefore, in the dialog you see the ShowsOrientation option ShowsOrientation .

Using multiple insert operations, you add options each time, not overwrite. You can also do it like this:

 operation.printPanel.options.insert([.showsPaperSize, .showsOrientation]) 

Finally, it also works to “set” parameters, and by providing existing parameters as the first value of the array, you get the effect of adding:

 operation.printPanel.options = [ operation.printPanel.options, .showsPaperSize, .showsOrientation ] 

(The first element of the operation.printPanel.options array means that the old parameters are indicated in the list of new parameters.)

0
source

All Articles