Drawing Text With Attributes(Chapter 20 of Cocoa Programming for Mac OS X)
1 #import "BigLetterView.h"
2
3
4 @implementation BigLetterView
5
6 - (IBAction)savePDF:(id)sender
7 {
8 NSSavePanel *panel = [NSSavePanel savePanel];
9 [panel setRequiredFileType:@"pdf"];
10 [panel beginSheetForDirectory:nil file:nil modalForWindow:[self window] modalDelegate:self didEndSelector:@selector(didEnd:returnCode:contextInfo:) contextInfo:NULL];
11 }
12
13 - (void)prepareAttributes
14 {
15 attributes = [[NSMutableDictionary alloc] init];
16 [attributes setObject:[NSFont fontWithName:@"Helvetica" size:75] forKey:NSFontAttributeName];
17 [attributes setObject:[NSColor redColor] forKey:NSForegroundColorAttributeName];
18 }
19
20 - (void)didEnd:(NSSavePanel *)sheet returnCode:(int)code contextInfo:(void *)contextInfo
21 {
22 if(code != NSOKButton)
23 {
24 return;
25 }
26
27 NSRect r = [self bounds];
28 NSData *data = [self dataWithPDFInsideRect:r];
29 NSString *path = [sheet filename];
30 NSError *error;
31 BOOL successful = [data writeToFile:path options:0 error:&error];
32 if (!successful)
33 {
34 NSAlert *a = [NSAlert alertWithError:error];
35 [a runModal];
36 }
37 }
38
39 - (id)initWithFrame:(NSRect)frameRect
40 {
41 if(![super initWithFrame:frameRect])
42 {
43 return nil;
44 }
45
46 NSLog(@"initializing view");
47 [self prepareAttributes];
48 bgColor = [[NSColor yellowColor] retain];
49 string = @" ";
50 return self;
51 }
52
53 - (void)dealloc
54 {
55 [bgColor release];
56 [string release];
57 [attributes release];
58 [super dealloc];
59 }
60
61 - (void)drawRect:(NSRect)dirtyRect
62 {
63 NSRect bounds = [self bounds];
64 [bgColor set];
65 [NSBezierPath fillRect:bounds];
66 [self drawStringCenteredIn:bounds];
67
68 if([[self window] firstResponder] == self)
69 {
70 [[NSColor keyboardFocusIndicatorColor] set];
71 [NSBezierPath setDefaultLineWidth:4.0];
72 [NSBezierPath strokeRect:bounds];
73 }
74 }
75
76 - (BOOL)isOpaque
77 {
78 return YES;
79 }
80
81 - (BOOL)acceptsFirstResponder
82 {
83 NSLog(@"Accepting");
84 return YES;
85 }
86
87 - (BOOL)resignFirstResponder
88 {
89 NSLog(@"Resigning");
90 [self setNeedsDisplay:YES];
91 return YES;
92 }
93
94 - (BOOL)becomeFirstResponder
95 {
96 NSLog(@"Becoming");
97 [self setNeedsDisplay:YES];
98 return YES;
99 }
100
101 - (void)keyDown:(NSEvent *)theEvent
102 {
103 [self interpretKeyEvents:[NSArray arrayWithObject:theEvent]];
104 }
105
106 - (void)insertText:(NSString *)input
107 {
108 [self setString:input];
109 }
110
111 - (void)insertTab:(id)sender
112 {
113 [[self window] selectKeyViewPrecedingView:self];
114 }
115
116 - (void)deleteBackward:(id)sender
117 {
118 [self setString:@" "];
119 }
120
121 #pragma mark Accessors
122
123 - (void)setBgColor:(NSColor *)c
124 {
125 [c retain];
126 [bgColor release];
127 bgColor = c;
128 [self setNeedsDisplay:YES];
129 }
130
131 - (NSColor *)bgColor
132 {
133 return bgColor;
134 }
135
136 - (void)drawStringCenteredIn:(NSRect)r
137 {
138 NSSize strSize = [string sizeWithAttributes:attributes];
139 NSPoint strOrigin;
140 strOrigin.x = r.origin.x + (r.size.width - strSize.width)/2;
141 strOrigin.y = r.origin.y + (r.size.height - strSize.height)/2;
142 [string drawAtPoint:strOrigin withAttributes:attributes];
143 }
144
145 - (void)setString:(NSString *)c
146 {
147 c = [c copy];
148 [string release];
149 string = c;
150 NSLog(@"The string is now %@", string);
151 [self setNeedsDisplay:YES];
152 }
153
154 - (NSString *)string
155 {
156 return string;
157 }
158
159 @end
2
3
4 @implementation BigLetterView
5
6 - (IBAction)savePDF:(id)sender
7 {
8 NSSavePanel *panel = [NSSavePanel savePanel];
9 [panel setRequiredFileType:@"pdf"];
10 [panel beginSheetForDirectory:nil file:nil modalForWindow:[self window] modalDelegate:self didEndSelector:@selector(didEnd:returnCode:contextInfo:) contextInfo:NULL];
11 }
12
13 - (void)prepareAttributes
14 {
15 attributes = [[NSMutableDictionary alloc] init];
16 [attributes setObject:[NSFont fontWithName:@"Helvetica" size:75] forKey:NSFontAttributeName];
17 [attributes setObject:[NSColor redColor] forKey:NSForegroundColorAttributeName];
18 }
19
20 - (void)didEnd:(NSSavePanel *)sheet returnCode:(int)code contextInfo:(void *)contextInfo
21 {
22 if(code != NSOKButton)
23 {
24 return;
25 }
26
27 NSRect r = [self bounds];
28 NSData *data = [self dataWithPDFInsideRect:r];
29 NSString *path = [sheet filename];
30 NSError *error;
31 BOOL successful = [data writeToFile:path options:0 error:&error];
32 if (!successful)
33 {
34 NSAlert *a = [NSAlert alertWithError:error];
35 [a runModal];
36 }
37 }
38
39 - (id)initWithFrame:(NSRect)frameRect
40 {
41 if(![super initWithFrame:frameRect])
42 {
43 return nil;
44 }
45
46 NSLog(@"initializing view");
47 [self prepareAttributes];
48 bgColor = [[NSColor yellowColor] retain];
49 string = @" ";
50 return self;
51 }
52
53 - (void)dealloc
54 {
55 [bgColor release];
56 [string release];
57 [attributes release];
58 [super dealloc];
59 }
60
61 - (void)drawRect:(NSRect)dirtyRect
62 {
63 NSRect bounds = [self bounds];
64 [bgColor set];
65 [NSBezierPath fillRect:bounds];
66 [self drawStringCenteredIn:bounds];
67
68 if([[self window] firstResponder] == self)
69 {
70 [[NSColor keyboardFocusIndicatorColor] set];
71 [NSBezierPath setDefaultLineWidth:4.0];
72 [NSBezierPath strokeRect:bounds];
73 }
74 }
75
76 - (BOOL)isOpaque
77 {
78 return YES;
79 }
80
81 - (BOOL)acceptsFirstResponder
82 {
83 NSLog(@"Accepting");
84 return YES;
85 }
86
87 - (BOOL)resignFirstResponder
88 {
89 NSLog(@"Resigning");
90 [self setNeedsDisplay:YES];
91 return YES;
92 }
93
94 - (BOOL)becomeFirstResponder
95 {
96 NSLog(@"Becoming");
97 [self setNeedsDisplay:YES];
98 return YES;
99 }
100
101 - (void)keyDown:(NSEvent *)theEvent
102 {
103 [self interpretKeyEvents:[NSArray arrayWithObject:theEvent]];
104 }
105
106 - (void)insertText:(NSString *)input
107 {
108 [self setString:input];
109 }
110
111 - (void)insertTab:(id)sender
112 {
113 [[self window] selectKeyViewPrecedingView:self];
114 }
115
116 - (void)deleteBackward:(id)sender
117 {
118 [self setString:@" "];
119 }
120
121 #pragma mark Accessors
122
123 - (void)setBgColor:(NSColor *)c
124 {
125 [c retain];
126 [bgColor release];
127 bgColor = c;
128 [self setNeedsDisplay:YES];
129 }
130
131 - (NSColor *)bgColor
132 {
133 return bgColor;
134 }
135
136 - (void)drawStringCenteredIn:(NSRect)r
137 {
138 NSSize strSize = [string sizeWithAttributes:attributes];
139 NSPoint strOrigin;
140 strOrigin.x = r.origin.x + (r.size.width - strSize.width)/2;
141 strOrigin.y = r.origin.y + (r.size.height - strSize.height)/2;
142 [string drawAtPoint:strOrigin withAttributes:attributes];
143 }
144
145 - (void)setString:(NSString *)c
146 {
147 c = [c copy];
148 [string release];
149 string = c;
150 NSLog(@"The string is now %@", string);
151 [self setNeedsDisplay:YES];
152 }
153
154 - (NSString *)string
155 {
156 return string;
157 }
158
159 @end