9

「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];
4

2 に答える 2

4

幸運なことに、Apple はスペルチェッカー クラスを提供しています: NSSpellChecker:

http://developer.apple.com/mac/library/documentation/cocoa/Conceptual/SpellCheck/Concepts/SpellChecker.html

これを使用すると、ユーザーが textdidChange デリゲート メソッドを使用してテキストを更新するたびに、スペルをチェックできます。

また、NSTextView ではなく NSTextField を使用したいと言っています。toggleAutomaticSpellingCorrection プロパティを設定できる編集可能な NSTextView を使用しないのはなぜですか?

編集:

メニュー項目の値をプログラムで変更するには、次の行に沿って何かを行います。

// 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];

// Actually perform menu action
[[autoSpellingMenuItem target] performSelector:[autoSpellingMenuItem action]];

編集:

上記のメソッドは、想定どおりにメソッドを実際に起動せず、ターゲットが NULL であるようです (最初のレスポンダーが設定されていないためですか?)。ただし、次のようにメッセージを直接送信することもできます。

// Focus TextField
[textField becomeFirstResponder];

// Enable Continous Spelling
NSTextView *textView = (NSTextView *)[self.window firstResponder];
[textView setContinuousSpellCheckingEnabled:YES];
于 2010-05-13T08:31:24.757 に答える
1

NSTextFieldデリゲートメソッドtextDidChangeを利用して、次を呼び出してみましたか?

range = [[NSSpellChecker sharedSpellChecker] checkSpellingOfString:aString startingAt:0];

毎回?

于 2010-05-13T08:23:46.790 に答える