Using NSFontPanel in Cocoa
Asked Answered
S

5

7

I'm trying to use an NSFontPanel to allow the user to change an application-wide font setting. It's supposed to work something like this: the user clicks a button, a font panel pops up, they choose a font and a size, and their selection is persisted.

The following code shows the panel:

- (IBAction)showFontMenu:(id)sender {
    NSFontManager *fontManager = [NSFontManager sharedFontManager];
    [fontManager setDelegate:self];

    NSFontPanel *fontPanel = [fontManager fontPanel:YES];
    [fontPanel makeKeyAndOrderFront:sender];
}

The documentation seems to suggest that the changeFont:(id)sender method should be called when the font changes; this isn't happening in my case.

- (void)changeFont:(id)sender {
    // blah
}

Any ideas on what I might be doing wrong?

Systematology answered 12/9, 2009 at 17:46 Comment(1)
This issue helped me: #6147805Sadyesaechao
P
2

The object you've defined -changeFont: on must the first responder or above it in the responder chain. You haven't specified where you've defined the method, but I assume it's on a controller object that is not in the responder chain.

Prewitt answered 12/9, 2009 at 17:54 Comment(1)
The View Controller's view is actually being loaded into another window; adding changeFont: to the parent's ViewController worked. Thanks for your help.Systematology
C
9

include this:

[fontManager setTarget:self];
Consensus answered 3/9, 2011 at 22:0 Comment(0)
P
2

The object you've defined -changeFont: on must the first responder or above it in the responder chain. You haven't specified where you've defined the method, but I assume it's on a controller object that is not in the responder chain.

Prewitt answered 12/9, 2009 at 17:54 Comment(1)
The View Controller's view is actually being loaded into another window; adding changeFont: to the parent's ViewController worked. Thanks for your help.Systematology
R
1

The core problem is this line:

[fontPanel makeKeyAndOrderFront:sender];

By making the font panel the key window, it's got no idea where to send action messages like -changeFont: to.

Runny answered 12/9, 2009 at 17:47 Comment(1)
I changed it to orderFrontFontPanel:; but the problem was actually with the responder chain. Thanks.Systematology
C
1

NSFontManager's delegate exists primarily to filter the fonts it supplies to the font panel via -fontManager:willIncludeFont:.

As kperryua mentions, -changeFont: is sent up the responder chain. The button that launches the font menu or its enclosing view might be a good place to put a responder for -changeFont:.

You might find the Font Handling guide marginally more useful than the Font Panel guide.

Chiliasm answered 13/9, 2009 at 2:8 Comment(1)
Correct link: Apple's Font HandlingLame
O
0

Here is my working implementation. Key point is you should make your object as the first responder where you have implemented - (void)changeFont:(id)sender

NSFontManager *fontManager = [NSFontManager sharedFontManager];

NSFontPanel *panel = [fontManager fontPanel:YES];
[[self window] makeFirstResponder:self];

[panel orderFront:self];

Simple changeFont: implementation Reference - Font Handling

-(void) changeFont:(id)sender
{
    //Here sender would be NSFontManager
    NSFont *oldFont = [self font];
    NSFont *newFont = [sender convertFont:oldFont];
    [self setFont:newFont];
}
Overgrowth answered 14/1, 2016 at 13:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.