Okay so I've checked all over S.O. and all over Google and I can't find what is wrong with my code:
- (IBAction)selectFont:(id)sender
{
NSFontManager *fontManager = [NSFontManager sharedFontManager];
[fontManager setDelegate:self];
[fontManager setTarget:self];
NSFontPanel *fontPanel = [fontManager fontPanel:YES];
[fontPanel makeKeyAndOrderFront:sender];
}
- (void)changeFont:(id)sender
{
NSFontManager *fontManager = [NSFontManager sharedFontManager];
font = [fontManager selectedFont];
NSLog(@"%@",[fontManager selectedFont]);
}
The font panel pops up, but when I select a font, the console returns (null)
for the selected font of the Font Manager. Anyone know what I'm missing?
Thanks
Apple's Documentation says that using
selectedFont
inchangeFont:
could result in unexpected behavior and "The use of selectedFont from within changeFont: is strongly discouraged." The appropriate way to get a font fromNSFontPanel
is to askNSFontManager
to convert the current font into the selected font. Here is an example application that properly usesNSFontPanel
:I've removed bits and pieces of the code that aren't relevant to your problem (such as IBOutlets to NSButtons and the NSWindow).
Please note this example uses ARC.
Another thing that is important to know is that the argument to
convertFont:
needs to be a validNSFont
instance. Otherwise,convertFont:
will return nil even though a font has been selected in theNSFontPanel
. For this reason, it's important to initialize theNSFont *font
instance variable to an validNSFont
instance.