开发者

Cocoa : How to make multiline NSTextField?

开发者 https://www.devze.com 2023-02-23 16:44 出处:网络
How to make multiline NSTextField? UPDATE: I\'ve found in IB special type of NSTextField called \"Wrapped Text Field\". It is multiline but wh开发者_高级运维en I want get a newline I have to press Ctr

How to make multiline NSTextField? UPDATE: I've found in IB special type of NSTextField called "Wrapped Text Field". It is multiline but wh开发者_高级运维en 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?


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.

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. 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.

[[yourTextView textStorage] setAttributedString:attrStr];
0

精彩评论

暂无评论...
验证码 换一张
取 消