Objective C Printing: How to set header content?
I want to print a specific NSView. When I do this, I wish to add content to the header of the print page.
e.g. If the NSView contains a picture of a cat, when I press print, print preview shows up 开发者_运维百科with the picture of the cat. I want the print out to be a picture of a cat, with the caption: "Cat" in the header, which I do not want visible on the original NSView.
Also, if this is possible, is it also possible to add images too?
Thanks!
You can overwrite the method - (NSAttributedString *)pageHeader
in your NSView
subclass. See Apple's documentation here.
Note that headers are generated only if the user defaults contain the key NSPrintHeaderAndFooter
with the value YES.
As for images, those can be added to a NSAttributedString
using a NSTextAttachment
.
If you want to draw things differently on the screen than the printer, you can use the isDrawingToScreen method in your drawRect: method.
Eg:
if (![[NSGraphicsContext currentContext] isDrawingToScreen]) {
//draw printer headers and images
}
精彩评论