1

次のことを行う特別なテキストフィールドが必要です:

  • マルチライン
  • タブキーのサポート
  • Enter キーが押されたときにアクションを送信する
  • Alt+Enter 改行
  • Shift+Enter 改行

何を使えばいいのかわからない。

NSTextView は良さそうに見えますが、Enter でアクションを設定できず、Enter キーを押すと新しい行が表示されます

NSTextField にはタブキーのサポートがなく、Shift-Enter は機能しません。

何か案は?ありがとう!

4

1 に答える 1

5

最善の策はNSTextView、必要な機能を取得するためにサブクラス化することです。簡単な例を次に示します。

MyTextView.h

@interface MyTextView : NSTextView
{
    id target;
    SEL action;
}
@property (nonatomic, assign) id target;
@property (nonatomic, assign) SEL action;
@end

MyTextView.m

@implementation MyTextView

@synthesize target;
@synthesize action;

- (void)keyDown:(NSEvent *)theEvent
{
    if ([theEvent keyCode] == 36) // enter key
    {
        NSUInteger modifiers = [theEvent modifierFlags];
        if ((modifiers & NSShiftKeyMask) || (modifiers & NSAlternateKeyMask))
        {
            // shift or option/alt held: new line
            [super insertNewline:self];
        }
        else
        {
            // straight enter key: perform action
            [target performSelector:action withObject:self];
        }
    }
    else
    {
        // allow NSTextView to handle everything else
        [super keyDown:theEvent];
    }
}

@end

ターゲットとアクションの設定は次のように行われます。

[myTextView setTarget:someController];
[mytextView setAction:@selector(omgTheUserPressedEnter:)];

NSResponderキーコードとのようなメッセージの完全なスイートの詳細については、キーコードinsertNewline:に関する私の質問に対する優れた回答を参照してくださいNSEvent。CocoaのNSEventクラスで使用するキーコードのリストはどこにありますか?

于 2010-12-18T22:00:21.287 に答える