简体   繁体   English

Cocoa:如何制作多行NSTextField?

[英]Cocoa : How to make multiline NSTextField?

How to make multiline NSTextField? 如何制作多行NSTextField? UPDATE: I've found in IB special type of NSTextField called "Wrapped Text Field". 更新:我在IB特殊类型的NSTextField中找到了“Wrapped Text Field”。 It is multiline but when I want get a newline I have to press Ctrl+Enter. 它是多行的但是当我想要换行时我必须按Ctrl + Enter。 But I want to press only Enter to get a newline. 但我想只按Enter键获取换行符。 How can I do it? 我该怎么做?

There is no way to specify this behavior solely in Interface Builder. 无法仅在Interface Builder中指定此行为。 You can do it with a delegate message as described in this tech note QA1454 . 您可以使用委托消息执行此操作,如本技术说明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;
}

Using NSTextView , its a multiline NSTextField sorta, it is a subclass of NSText correct my if I am wrong. 使用NSTextView ,它是一个多行NSTextField sorta,它是NSText的子类,如果我错了, NSText更正我的。 The NSTextView has an NSTextStorage , which is a subclass of NSAttributedString . NSTextView有一个NSTextStorage ,它是NSAttributedString的子类。 You need to give it an NSAttributedString object instead of a NSString to fill its contents as it can display colors etc. 你需要给它一个NSAttributedString对象而不是NSString来填充它的内容,因为它可以显示颜色等。

[[yourTextView textStorage] setAttributedString:attrStr];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM