objective-c - 无论如何让(包装)NSTextField 在按下回车键时写一个回车?

标签 objective-c cocoa nstextfield

我想在我的应用程序中使用可能包含回车符的环绕文本字段。有什么方法可以强制 NSTextField 对象在文本区域写入回车符,而不是在按下 Return 键时将其操作发送到目标?

最佳答案

这包含在 Technical Q&A QA1454 中,其中还列举了为什么在这种情况下会使用 NSTextField 而不是 NSTextView 的原因。

您可以在文本字段委托(delegate)中实现以下方法:

- (BOOL)control:(NSControl*)control
    textView:(NSTextView*)textView
    doCommandBySelector:(SEL)commandSelector
{
    BOOL result = NO;

    if (commandSelector == @selector(insertNewline:))
    {
        // new line action:
        // always insert a line-break character and don’t cause the receiver
        // to end editing
        [textView insertNewlineIgnoringFieldEditor:self]; 
        result = YES;
    }

    return result;
}

关于objective-c - 无论如何让(包装)NSTextField 在按下回车键时写一个回车?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6117032/

相关文章:

ios - Objective-C - 左下角的文本基线

macos - 禁用恢复

cocoa - Cocoa 中有类似 [aCalCalendar setType] 的东西吗?

objective-c - 将 NSTextField 的文本重置为空字符串 -- setText : doesn't work

iphone - FBNativeDialogs 中缺少链接、名称、标题和描述

objective-c - 是|管道字符制作 URLWithString : fail?

objective-c - 类扩展中的 IBAction 方法?

objective-c - 当 TextView 中超过 25 个字符时提醒用户

cocoa - NSTextField : How to draw background only when focused

swift - 稍微延迟编辑文本字段