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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 07:56:16  来源:igfitidea点击:

Cocoa : How to make multiline NSTextField?

objective-ccocoamacos

提问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 NSTextFieldsorta, it is a subclass of NSTextcorrect my if I am wrong. The NSTextViewhas an NSTextStorage, which is a subclass of NSAttributedString. You need to give it an NSAttributedStringobject instead of a NSStringto fill its contents as it can display colors etc.

使用NSTextView,它是一个多行NSTextField排序,NSText如果我错了,它是更正我的子类。该NSTextView具有一个NSTextStorage,这是的一个子类NSAttributedString。你需要给它一个NSAttributedString对象而不是一个NSString来填充它的内容,因为它可以显示颜色等。

[[yourTextView textStorage] setAttributedString:attrStr];