I like composing controls and such over coming up with new views. This frustrates me with printing, though, since it’s based on making a view, and making sure that is page-cuttable. I made RTF-export for my app in the first place because I thought there was an easy conversion to printing.
Poking around Stack Overflow, I realized that the view to print can be an existing class. I could couple the document-data-to-NSAttributedString operation I already wrote with a NSTextView to make something that a NSPrintOperation could use: > override func printOperation(withSettings printSettings: [String : Any]) > throws -> NSPrintOperation { > // Copy the message to a string to print. > var messageString: NSAttributedString! = /* … */ > > // Create a view to visualize the message string. > var info = self.printInfo.copy() as! NSPrintInfo > info.dictionary().addEntries(from: printSettings) > > let view = NSTextView(frame: info.imageablePageBounds) > view.autoresizingMask = [.viewWidthSizable, .viewHeightSizable] > do { > view.textStorage?.beginEditing() > defer { view.textStorage?.endEditing() } > > view.textStorage?.setAttributedString(messageString) > } > > return NSPrintOperation(view: view, printInfo: info) > } I hope it’s a nice first try, but I have some concerns. 1. I also added an “Export as PDF…” menu item, which uses the save-as-PDF default action. I guess it calls my code, but the docs for “NSDocument.pdfPrintOperation” say I should use a separate print-information objects for PDF mode. Does the default code take care of that already? Or do I have to figure out somehow if I’m in print-to-PDF mode or regular print mode and change the initialization of “info” above to be “self.pdfPrintOperation” as needed? 2. The RTF files I saved with the conversion operation have wrapping when viewed through QuickLook. But the print-outs don’t do that; they do wrap, but the limit is past the page margins so I lose part of the text. How can I set the wrapping-mode/margins correctly. 3. Are there any other (subtle) problems with the text-view code, since I just took it from Stack Overflow? — Daryle Walker Mac, Internet, and Video Game Junkie darylew AT mac DOT com _______________________________________________ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) Please do not post admin requests or moderator comments to the list. Contact the moderators at cocoa-dev-admins(at)lists.apple.com Help/Unsubscribe/Update your Subscription: https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com