以编程方式制作多行标签

发布于 2025-01-07 16:35:22 字数 989 浏览 3 评论 0原文

我需要以编程方式制作一些标签和文本字段。我可以到达那里。

    //create the file name label
    NSTextField* newFileNameLabel = [[NSTextField alloc] initWithFrame:NSMakeRect(objXPos, objYPos, 300.0, 20.0)];
    //set properties
    [newFileNameLabel setBordered: NO];
    [newFileNameLabel setTextColor: [NSColor whiteColor]];
    [newFileNameLabel setDrawsBackground:NO];
    [newFileNameLabel setEditable:NO];
    [newFileNameLabel setStringValue: @"File Name:"];

    [vSheetView addSubview:newFileNameLabel];

但我在文档中找不到任何可以让我设置换行属性的内容。在 IB 中,属性是布局,其选项为“滚动、换行和截断”。 NSTextField 没有方法来设置它,如果我沿着继承链向上,NSControl 也没有。 NSView 有一个 setNeedsLayout 方法,但它似乎不相关:

You only ever need to invoke this method if your view implements custom layout 
not expressible in the constraint-based layout system by overriding the layout method. 
The system invokes this method automatically for all views using constraints for layout.

NSTextFieldCell 也没有任何方法。任何帮助将不胜感激。

I need to programmatically make some labels and text fields. I can get there.

    //create the file name label
    NSTextField* newFileNameLabel = [[NSTextField alloc] initWithFrame:NSMakeRect(objXPos, objYPos, 300.0, 20.0)];
    //set properties
    [newFileNameLabel setBordered: NO];
    [newFileNameLabel setTextColor: [NSColor whiteColor]];
    [newFileNameLabel setDrawsBackground:NO];
    [newFileNameLabel setEditable:NO];
    [newFileNameLabel setStringValue: @"File Name:"];

    [vSheetView addSubview:newFileNameLabel];

But I couldn't find anything in the docs that would let me set a wrap property. In IB the property is layout with 'scroll, wrap and truncate' as its options. NSTextField doesn't have a method to set this, and if I go up the inheritance chain neither does NSControl. NSView has a setNeedsLayout method but it doesn't seem related:

You only ever need to invoke this method if your view implements custom layout 
not expressible in the constraint-based layout system by overriding the layout method. 
The system invokes this method automatically for all views using constraints for layout.

NSTextFieldCell doesn't have any methods either. Any help would be appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

迟到的我 2025-01-14 16:35:22

在 OS X 10.11 和 Swift 2.1 中,以下内容对我有帮助:

multilineLabel.lineBreakMode = .ByWordWrapping
multilineLabel.setContentCompressionResistancePriority(250, forOrientation: .Horizontal)

如果使用自动布局,则需要设置压缩阻力。

In OS X 10.11 and Swift 2.1 the following helped me:

multilineLabel.lineBreakMode = .ByWordWrapping
multilineLabel.setContentCompressionResistancePriority(250, forOrientation: .Horizontal)

Setting the compression resistance is required if you use AutoLayout.

没企图 2025-01-14 16:35:22

这对我有用:

textField.usesSingleLineMode = false
textField.cell?.wraps = true
textField.cell?.scrollable = false

This works for me:

textField.usesSingleLineMode = false
textField.cell?.wraps = true
textField.cell?.scrollable = false
无声静候 2025-01-14 16:35:22

未经测试,但 NSTextFieldCellsetWraps: (继承自 NSCell)应该执行您所追求的操作。

要获取单元格,请将 cell 方法发送到文本字段。 NSTextField 继承了 NSControlcell 方法,因此如果要阅读文档,需要在文档中搜索 NSControl< /代码>。

有关单元格如何在 NSCell 文档

Not tested, but setWraps: of NSTextFieldCell (inherited from NSCell) should do what you are after.

To get the cell, send the cell method to the text field. NSTextField inherits the cell method from NSControl, so if you want to read the docs, you need to search the docs for NSControl.

More about how cells work in the NSCell docs.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文