开发者

What object to use for special text-editing

i need a special text-field that should do the following stuff:

  • multiline
  • tab key support
  • send action when enter key gets pressed
  • alt+enter for new line
  • shift+enter for new line
开发者_JAVA技巧

i don't know what to use.

NSTextView looks good, but i can not set an action on enter and pressing the enter key results in a new line

NSTextField has no tab-key support and shift-enter is not working.

any ideas? thanks!


Your best bet is to subclass NSTextView to get the functionality that you want. Here is a quick example:

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

Setting the target and action would be done as follows:

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

For more details on key codes and the full suite of NSResponder messages like insertNewline:, see the excellent answers to my question about NSEvent keycodes: Where can I find a list of key codes for use with Cocoa's NSEvent class?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜