本文介绍了无论如何使一个(包装)NSTextField写回车回车按回车键?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想使用可能在我的应用程序中包含回车的包装文本字段。是否有任何方法强制NSTextField对象写入一个回车到文本区域,而不是在按下返回键时将其操作发送到其目标?
I want to use a wrapping text field that can potentially contain carriage returns in my app. Is there any way to force the NSTextField object to write a carriage return into the text area instead of sending its action to its target when the Return key is pressed?
推荐答案
这在中有介绍,它还列举了在这种情况下,为什么要使用 NSTextField
而不是 NSTextView
的原因。
This is covered in Technical Q&A QA1454, which also enumerates reasons why one would use NSTextField
instead of NSTextView
in this case.
您可以在文本字段委托中实现以下方法:
You can implement the following method in the text field 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;
}
这篇关于无论如何使一个(包装)NSTextField写回车回车按回车键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!