3

I have an application where I use the NSFontPanel. I open the font panel like this:

NSFontManager *fontManager = [NSFontManager sharedFontManager];
[fontManager orderFrontFontPanel:self];
[fontManager setDelegate:self];
[fontManager setTarget:self];
[fontManager setAction:@selector(changeFont:)];

I then have an -(void)changeFont:(id)sender method which reacts to font changes in the panel. This all works well.

Now, I want to disable some of the font options, so I implement the validModesForFontPanel method of the NSFontPanelValidation protocol:

- (NSUInteger)validModesForFontPanel:(NSFontPanel *)fontPanel {
    NSLog(@"validModesForFontPanel");
    return NSFontPanelFaceModeMask | NSFontPanelCollectionModeMask | NSFontPanelSizeModeMask;
}

However, the method never gets called. In the documentation it says: "This message is sent up the responder chain to the first responder implementing the method. Ideally that object should be the first responder found that also implements changeFont:."

I do implement changeFont in this responder, so unless there is some other responder which I am not aware of, I don't know what happens to this message.

Does anyone have any suggestions?

pajevic
  • 4,607
  • 4
  • 39
  • 73

1 Answers1

4

You arent indicating any responder for the panel, just for the manager.
this should do it:

NSFontPanel* fontPanel = [fontManager fontPanel:YES];
[fontPanel setDelegate:self];    
[fontPanel makeKeyAndOrderFront:sender];    
JPed
  • 196
  • 6
  • Hi JPed, thank you for your reply. It was my understanding that everything went through the `NSFontManager`, but I guess I was wrong. Anyway, the code above does not work as `NSFontPanel`'s delegate must be an instance of `NSWindowDelegate`, and `self` is a subclass of `NSViewController`. It gives me this warning: `warning: sending 'TextViewController *const __strong' to parameter of incompatible type 'id'` – pajevic Feb 22 '12 at 21:41
  • Hi again. I was a bit to quick when I said it didn't work. It actually does, in spite of the fact that I get the warning. Is this something you have encountered? – pajevic Feb 23 '12 at 07:36
  • I finally figured it out. The warning came because my class wasn't implementing the `NSWindowDelegate` protocol. I just wish all of this was a little better documented. Anyway, thank you for your answer, it did the trick :D – pajevic Feb 23 '12 at 07:44
  • This solution works, but `validModesForFontPanel` gets called many many times while the font panel is open - add an `NSLog` in it to see that happens. – Ethan Nov 12 '14 at 22:22
  • this is great. But I don't get it... so many comments about it working and I am the first to vote this up? – Radu Simionescu Feb 04 '16 at 22:11