Cocoa:如何制作多行NSTextField?

Sia*_*sou 12 macos cocoa objective-c

如何制作多行NSTextField?更新:我在IB特殊类型的NSTextField中找到了"Wrapped Text Field".它是多行的但是当我想要换行时我必须按Ctrl + Enter.但我想只按Enter键获取换行符.我该怎么做?

Jon*_*etz 11

无法仅在Interface Builder中指定此行为.您可以使用委托消息执行此操作,如本技术说明QA1454中所述.

以下是技术说明中的示例委托消息:

- (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;
}
Run Code Online (Sandbox Code Playgroud)


Ant*_*udt 6

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

[[yourTextView textStorage] setAttributedString:attrStr];
Run Code Online (Sandbox Code Playgroud)