Pregunta

I'm trying to trap the CMD+ and CMD- keyboard actions in my NSView - to increase and decrease the font size of some custom text. For various reasons, I can't simply use an NSTextView. Does anyone know the correct way to handle this?

As I understand it, they are key equivalents for menu items. I can 'enable' the menu items by implementing changeFont: and I can trap the keypress by implementing performKeyEquivalent: but this seems a little bit of a hack. Also when changeFont does get called, it's not clear how I interpret the fact that it was called for CMD+/- since it simply sends me an NSFontManager. Makes me wonder if I'm handling the wrong message?

Thanks in advance.

¿Fue útil?

Solución

Gerd (see comments above) got me sorted. In my changeFont handler I create a temp font of size 10 then pass it back to the font manager and check the size of the returned font. If it's bigger then CMD+ was pressed, if it was smaller then CMD- was pressed.

- (void)changeFont:(id)sender
{
    NSFontManager* fm = sender;

    NSFont* fntBefore = [NSFont systemFontOfSize:10];
    NSFont* fntAfter = [fm convertFont:fntBefore];

    CGFloat delta = fntAfter.pointSize - fntBefore.pointSize;
}
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top