macos Cocoa:如何制作多行 NSTextField?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5611292/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Cocoa : How to make multiline NSTextField?
提问by Siarhei Fedartsou
How to make multiline NSTextField? UPDATE:I've found in IB special type of NSTextField called "Wrapped Text Field". It is multiline but when I want get a newline I have to press Ctrl+Enter. But I want to press only Enter to get a newline. How can I do it?
如何制作多行 NSTextField?更新:我在 IB 中发现了一种特殊类型的 NSTextField,称为“Wrapped Text Field”。它是多行的,但是当我想要换行时,我必须按 Ctrl+Enter。但我只想按 Enter 以获得换行符。我该怎么做?
采纳答案by Jon Steinmetz
There is no way to specify this behavior solely in Interface Builder. You can do it with a delegate message as described in this tech note QA1454.
无法单独在 Interface Builder 中指定此行为。您可以使用此技术说明QA1454 中所述的委托消息来执行此操作。
Here is the example delegate message from the tech note:
以下是技术说明中的示例委托消息:
- (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;
}
else if (commandSelector == @selector(insertTab:))
{
// tab action:
// always insert a tab character and don't cause the receiver to end editing
[textView insertTabIgnoringFieldEditor:self];
result = YES;
}
return result;
}
回答by Antwan van Houdt
Using NSTextView
, its a multiline NSTextField
sorta, it is a subclass of NSText
correct my if I am wrong. The NSTextView
has an NSTextStorage
, which is a subclass of NSAttributedString
. You need to give it an NSAttributedString
object instead of a NSString
to fill its contents as it can display colors etc.
使用NSTextView
,它是一个多行NSTextField
排序,NSText
如果我错了,它是更正我的子类。该NSTextView
具有一个NSTextStorage
,这是的一个子类NSAttributedString
。你需要给它一个NSAttributedString
对象而不是一个NSString
来填充它的内容,因为它可以显示颜色等。
[[yourTextView textStorage] setAttributedString:attrStr];