Scott Nicol
Scott Nicol

Reputation: 11808

NSTextField new line operations

I would like my NSTextField to add another line on enter (rather that option-enter). Is there any way to do that at all?

Cheers!

Upvotes: 6

Views: 2696

Answers (2)

Lightness Races in Orbit
Lightness Races in Orbit

Reputation: 385098

Yep — by overriding control in a bound NSTextFieldDelegate you can intercept an enter keypress and make it do what you want.

Here's an example adapted from the article "How to make NSTextField accept tab, return and enter keys." on Apple's developer portal:

(BOOL)control:(NSControl*)control textView:(NSTextView*)textView doCommandBySelector:(SEL)commandSelector
{
    if (commandSelector == @selector(insertNewline:))
    {
        // Insert a line-break character and don't cause the receiver
        // to end editing
        [textView insertNewlineIgnoringFieldEditor:self];
        return YES;
    }

    return NO;
}

Weirdly, that NSTextView* is not a typo, even though we're dealing with NSTextFields.

Upvotes: 5

Mudit Bajpai
Mudit Bajpai

Reputation: 3020

NSTextField supports new line breaks by using Option-Return or Option-Enter. But under most circumstances the easiest solution would be to use NSTextView instead.

Upvotes: 6

Related Questions