Hello all. Working my way through Hillegass, I find myself stuck on Chapter 20, Challenge 2, along with a few general questions. I've found several solutions to this challenge but none seem correct.
Background:
The program is a simple Text Drawing Application - with two text boxes and one large Custom View that display's whatever Key you press. The image View draw's from a string (NSString), and attributes are added to it via an 'attributes' mutable dictionary.
In the challenge, Hillegass wants you to add checkbox's to make the text bold and / or italic:
2 checkboxe's (one for bold, one for italic)
2 BOOL values (bold, italic).
Use NSFontManager, with the sample code given:
fontManager = [NSFontManager sharedFontManager];
boldFont = [fontManager convertFont:aFont toHaveTrait:NSBoldFontMask];
The relevant Code up to this point:
BigLetterView.h:
With 'prepareAttributes' called in the 'init' function.
One solution I found, invovling the prepareAttributes function:
With this method being called whenever the bold or italic setter's are called. However, I would think this would leak memory (specifically the new *font pointer, and a new 'attributes' variable).
I was wandering if another way would be to simply, inside the 'bold' and 'italic' setter's, have them:
1. Set themselves to new value
2. Check that value, use 'if' statement to then set the font to bold or un-bold, using the following Code:
So, with this solution, I'm trying to find out if I am leaking memory, OR if there is a more elegant way to write this - it seems having to call all this code each time I call the setter is excessive. Hillegass makes no indication that I should have to declare new variables (for newFont, for instance) in my BigLetterView header file.
Any and all suggestions or criticism's appreciated.
Background:
The program is a simple Text Drawing Application - with two text boxes and one large Custom View that display's whatever Key you press. The image View draw's from a string (NSString), and attributes are added to it via an 'attributes' mutable dictionary.
In the challenge, Hillegass wants you to add checkbox's to make the text bold and / or italic:
2 checkboxe's (one for bold, one for italic)
2 BOOL values (bold, italic).
Use NSFontManager, with the sample code given:
fontManager = [NSFontManager sharedFontManager];
boldFont = [fontManager convertFont:aFont toHaveTrait:NSBoldFontMask];
The relevant Code up to this point:
BigLetterView.h:
Code:
@interface BigLetterView : NSView {
NSColor *bgColor;
[B]NSString *string;
NSMutableDictionary *attributes;
BOOL *italic;
BOOL *bold;[/B]
}
@property (readwrite, retain) NSColor *bgColor;
@property (copy, readwrite) NSString *string;
- (void)prepareAttributes;
- (void)drawStringCenteredIn:(NSRect)r;
- (IBAction)savePDF:(id)sender;
- (IBAction)italicCheckbox:(id)sender;
- (IBAction)boldCheckbox:(id)sender;
With 'prepareAttributes' called in the 'init' function.
One solution I found, invovling the prepareAttributes function:
Code:
- (void)prepareAttributes {
attributes = [[NSMutableDictionary alloc] init];
NSFont *font = [NSFont fontWithName:@"Helvetica" size:75];
[B]NSFontManager *fontManager = [NSFontManager sharedFontManager];
if (italic) font = [fontManager convertFont:font toHaveTrait:NSItalicFontMask];
if (bold) font = [fontManager convertFont:font toHaveTrait:NSBoldFontMask];
[attributes setObject:font
forKey:NSFontAttributeName];[/B]
[attributes setObject:[NSColor redColor]
forKey:NSForegroundColorAttributeName];
[self setNeedsDisplay:YES];
}
With this method being called whenever the bold or italic setter's are called. However, I would think this would leak memory (specifically the new *font pointer, and a new 'attributes' variable).
I was wandering if another way would be to simply, inside the 'bold' and 'italic' setter's, have them:
1. Set themselves to new value
2. Check that value, use 'if' statement to then set the font to bold or un-bold, using the following Code:
Code:
NSFontManager *fontManager = [NSFontManager sharedFontManager];
NSFont *newFont;
newFont = [fontManager convertFont:[attributes valueForKey:NSFontAttributeName] toHaveTrait:NSBoldFontMask];
[attributes setObject:newFont forKey:NSFontAttributeName];
[self setNeedsDisplay:YES];
So, with this solution, I'm trying to find out if I am leaking memory, OR if there is a more elegant way to write this - it seems having to call all this code each time I call the setter is excessive. Hillegass makes no indication that I should have to declare new variables (for newFont, for instance) in my BigLetterView header file.
Any and all suggestions or criticism's appreciated.