NSTextField 中的拼写建议

Spelling suggestions in NSTextField

我的应用有 NSTextFields 用于输入;我故意不使用 NSNumberFormatter 来对输入进行特殊处理。该应用程序实现 "full screen" 模式。当应用程序处于全屏状态且焦点位于文本字段中时,我按 ESC 键恢复窗口模式,但我收到了一个拼写为 suggestions/completions 的弹出窗口。当按下 ESC 键时,我不希望出现以下任何一种行为:完成弹出窗口,也不想退出全屏模式。有什么建议么?谢谢。

您需要设置一个 NSTextFieldDelegate 来处理该命令,并在文本字段上设置委托。这是一个例子:

@property (weak) IBOutlet NSWindow *window;
@property (weak) IBOutlet NSTextField *textField;
@end

@implementation AppDelegate

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
    // Insert code here to initialize your application
    self.textField.delegate = self;
}

- (BOOL)control:(NSControl*)control textView:(NSTextView*)textView doCommandBySelector:(SEL)commandSelector {
    if (commandSelector == @selector(cancelOperation:)) {
        NSLog(@"handleCancel");
        return YES;
    }
    return NO;
}

```

如果您只是想消除拼写建议,您可以重写以下方法,但上面的方法都可以。

- (NSArray *)control:(NSControl *)control textView:(NSTextView *)textView completions:(NSArray *)words forPartialWordRange:(NSRange)charRange indexOfSelectedItem:(NSInteger *)index {
return nil;
}

这就是我实现我想要的行为的方式:

- (BOOL)control:(NSControl *)control textView:(NSTextView *)textView doCommandBySelector:(SEL)commandSelector {

    if (commandSelector == @selector(cancelOperation:)) {

        if (([_window styleMask] & NSFullScreenWindowMask) == NSFullScreenWindowMask) {

            [textView doCommandBySelector:@selector(toggleFullScreen:)];
        }

        return YES;
    }

    return NO;
}