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!
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.
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 NSTextField
s.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With