Re: NSTextView - printing page numbers and headers
Re: NSTextView - printing page numbers and headers
- Subject: Re: NSTextView - printing page numbers and headers
- From: Todd Ransom <email@hidden>
- Date: Mon, 3 Oct 2005 17:37:45 -0600
This code was adapted from a class given to me by Bill Cheeseman. It
is a text view that will tack on page numbers, document title, and
author name when it is printed. The interesting stuff is in
drawPageBorderWithSize: which Bill figured out does not work as
documented (at the time, I think the docs are better now).
Todd Ransom
Return Self Software
http://returnself.com
#import <Cocoa/Cocoa.h>
@interface AVNRprintTextView : NSTextView
{
NSPrintInfo *printInfo;
NSMutableDictionary *borderTextAttributes;
NSMutableAttributedString *printString;
int pageNumber;
id prefs;
NSString *title; // the title
}
- (NSMutableDictionary *)borderTextAttributes;
- (void)setBorderTextAttributes:(NSMutableDictionary *)attributes;
- (NSString *)title;
- (void)setTitle:(NSString *)newTitle;
- (id)initWithFrame:(NSRect)frame;
- (void)drawPageBorderWithSize:(NSSize)borderSize;
@end
#import "AVNRprintTextView.h"
@implementation AVNRprintTextView
- (id)initWithFrame:(NSRect)frame { // designated initializer
if ((self = [super initWithFrame:frame])) {
printInfo = [NSPrintInfo sharedPrintInfo];
[printInfo setLeftMargin:72];
[printInfo setRightMargin:72];
[printInfo setTopMargin:72];
[printInfo setBottomMargin:72];
[printInfo setVerticallyCentered:NO];
prefs = [[[NSUserDefaultsController
sharedUserDefaultsController] values] retain];
}
return self;
}
#pragma mark Accessors
- (NSMutableDictionary *)borderTextAttributes {
return [[borderTextAttributes retain] autorelease];
}
- (void)setBorderTextAttributes:(NSMutableDictionary *)attributes {
if (borderTextAttributes != attributes) {
[borderTextAttributes release];
borderTextAttributes = [attributes copy];
}
}
- (NSString *)title {
return title;
}
- (void)setTitle:(NSString *)newTitle {
[title autorelease];
title = [newTitle retain];
}
#pragma mark Printing
- (void)drawPageBorderWithSize:(NSSize)borderSize {
NSMutableString *headerString = [[NSMutableString alloc] init];
NSString *titleString = [self title];
// Create attributes dictionary for drawing border text.
borderTextAttributes = [[NSMutableDictionary alloc] init];
[borderTextAttributes setObject:[NSFont
fontWithName:@"Helvetica" size:10.0] forKey:NSFontAttributeName];
// Temporarily set print view frame size to border size (paper
size), to print in margins.
NSRect savedFrame = [self frame];
[self setFrame:NSMakeRect(0, 0, borderSize.width,
borderSize.height)];
// NO == 0 but YES is only guaranteed to be non-zero for
[NSNumber boolValue]
if ([[prefs valueForKey:@"printName"] boolValue] != NO) {
[headerString appendString: [prefs valueForKey:@"authorName"]];
}
if ([[prefs valueForKey:@"printTitle"] boolValue] != NO) {
if (![headerString isEqual: @""])
[headerString appendString: @"/"];
[headerString appendString: titleString];
}
if ([[prefs valueForKey:@"printPageNums"] boolValue] != NO) {
pageNumber = [[NSPrintOperation currentOperation] currentPage];
NSString *pageNumberString = [NSString stringWithFormat:@"%
d", pageNumber];
if (![headerString isEqual: @""]) {
[headerString appendString: @"/"];
}
[headerString appendString: pageNumberString];
}
if (![headerString isEqual: @""]) {
// Draw right header in top margin.
NSSize headerStringSize = [headerString sizeWithAttributes:
[self borderTextAttributes]];
float headerStringX = borderSize.width - [printInfo
rightMargin] - headerStringSize.width;
float headerStringY = headerStringSize.height * 2;
[self lockFocus];
[headerString
drawAtPoint:NSMakePoint(headerStringX, headerStringY)
withAttributes:[self borderTextAttributes]];
[self unlockFocus];
// Restore print view frame size.
[self setFrame:savedFrame];
}
[headerString release];
}
- (void)dealloc {
[borderTextAttributes autorelease];
[title autorelease];
[prefs release];
[printString release];
[super dealloc];
}
On Oct 3, 2005, at 4:02 PM, Keith Blount wrote:
Hello,
Is there an easy way to print page numbers and header
text when printing an NSTextView that still allows you
take advantage of NSTextView's built-in pagination?
Many thanks for any advice,
Keith
__________________________________
Yahoo! Mail - PC Magazine Editors' Choice 2005
http://mail.yahoo.com
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden