Select a font in NSFontPanel

2.6k Views Asked by At

I would like to open the NSFontPanel with a font (in the following example Helevtica) selected initially. I mean that when the font panel shows up, the collection and family values are selected. Here is my code:

NSFontManager * fontManager = [NSFontManager sharedFontManager];
[fontManager setTarget:self];
[fontManager setSelectedFont:[NSFont fontWithName:@"Helvetica" size:150.0]; isMultiple:NO];
[fontManager orderFrontFontPanel:self];

Why does the Helvetica font not show up selected in the font panel?

I am also using NSFontPanelValidation protocol, here is the code:

- (NSUInteger)validModesForFontPanel:(NSFontPanel *)fontPanel
{
    return NSFontPanelFaceModeMask | NSFontPanelCollectionModeMask | NSFontPanelUnderlineEffectModeMask | NSFontPanelStrikethroughEffectModeMask;
}
1

There are 1 best solutions below

0
the Reverend On

On a fresh appkit project, your code works for me. I'm calling it from the main delegate. Helvetica at 150 pts gets selected when I open it without using validation.

enter image description here