Cocoa:如何制作多行NSTextField?

时间:2022-09-06 19:25:38

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键获取换行符。我该怎么做?

2 个解决方案

#1


11  

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;
}

#2


6  

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 sorta,它是NSText的子类,如果我错了,请更正我的。 NSTextView有一个NSTextStorage,它是NSAttributedString的子类。你需要给它一个NSAttributedString对象而不是NSString来填充它的内容,因为它可以显示颜色等。

[[yourTextView textStorage] setAttributedString:attrStr];

#1


11  

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;
}

#2


6  

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 sorta,它是NSText的子类,如果我错了,请更正我的。 NSTextView有一个NSTextStorage,它是NSAttributedString的子类。你需要给它一个NSAttributedString对象而不是NSString来填充它的内容,因为它可以显示颜色等。

[[yourTextView textStorage] setAttributedString:attrStr];