以编程方式制作多行标签
我需要以编程方式制作一些标签和文本字段。我可以到达那里。
//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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在 OS X 10.11 和 Swift 2.1 中,以下内容对我有帮助:
如果使用自动布局,则需要设置压缩阻力。
In OS X 10.11 and Swift 2.1 the following helped me:
Setting the compression resistance is required if you use AutoLayout.
这对我有用:
This works for me:
未经测试,但
NSTextFieldCell
的setWraps:
(继承自NSCell
)应该执行您所追求的操作。要获取单元格,请将
cell
方法发送到文本字段。NSTextField
继承了NSControl
的cell
方法,因此如果要阅读文档,需要在文档中搜索NSControl< /代码>。
有关单元格如何在 NSCell 文档。
Not tested, but
setWraps:
ofNSTextFieldCell
(inherited fromNSCell
) should do what you are after.To get the cell, send the
cell
method to the text field.NSTextField
inherits thecell
method fromNSControl
, so if you want to read the docs, you need to search the docs forNSControl
.More about how cells work in the NSCell docs.