「as-you-type」スペルチェックを有効にしたい NSTextField があります。アプリケーションをロードするときに、メニュー バー > [編集] > [スペルと文法] > [入力中にスペルをチェック] からこれを行うことができます。
このオプションをデフォルトで有効にしたいと思います。IB 内で NSTextView に対してこれを有効にできますが、UI のこの部分には NSTextField を使用したいと思います。
ありがとうございました。
更新: Objective-C コードから NSTextField の [Menu Bar] > [Edit] > [Spelling and Grammar] > [Check Spelling While Typing] オプションをプログラムで実行できるかどうかを知っている人はいますか? NSTextField は "Check Spelling While Typing" オプションをサポートしているようですが、Obj-C からオプションを有効にする方法はありません。
編集#1
メニューを手動で有効にするために次のことを試みましたが、機能しませんでした。
// Focus TextField
[textField becomeFirstResponder];
// Enable Spell Checking
NSMenu *mainMenu = [[NSApplication sharedApplication] mainMenu];
NSMenu *editMenu = [[mainMenu itemWithTitle:@"Edit"] submenu];
NSMenu *spellingMenu = [[editMenu itemWithTitle:@"Spelling and Grammar"] submenu];
NSMenuItem *autoSpellingMenuItem = [spellingMenu itemWithTitle:@"Check Spelling While Typing"];
[autoSpellingMenuItem setEnabled:YES];
NSLog(@"Menu: %@", [autoSpellingMenuItem description]);
NSLog(@"Target: %@", [[autoSpellingMenuItem target] description]);
// Actually perform menu action
[[autoSpellingMenuItem target] performSelector:[autoSpellingMenuItem action]];
setEnabled:YES を使用するのではなく、メニュー項目アクションを直接呼び出すことはできませんか?
上記は以下を出力しますが、ターゲットが null である理由がわかりません
App[3895:a0f] Menu: <NSMenuItem: 0x100135180 Check Spelling While Typing>
Current language: auto; currently objective-c
App[3895:a0f] Target: (null)
解決
以下は、他の誰かが知る必要がある場合のこの問題の解決策です。一部の NSLogging は、NSTextField を firstResponder に設定した後、firstResponder に実際に NSTextView が含まれていると、スペルを有効にできることを示しました。NSTextField には、レスポンダーを取得するサブビューに NSTextView が含まれていると想定しています。実際には、これは NSTextField クラスで公開する必要があります。
// Focus TextField
[textField becomeFirstResponder];
// Enable Continous Spelling
NSTextView *textView = (NSTextView *)[self.window firstResponder];
[textView setContinuousSpellCheckingEnabled:YES];