Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 419
Scott Nicol Avatar asked Feb 16 '12 16:02

Scott Nicol


2 Answers

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.

like image 175
Mudit Bajpai Avatar answered Nov 12 '22 21:11

Mudit Bajpai


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.

like image 4
Lightness Races in Orbit Avatar answered Nov 12 '22 21:11

Lightness Races in Orbit