创建 NSBox 运行时示例

发布于 2024-11-13 07:54:54 字数 74 浏览 3 评论 0原文

请问您能否举例说明如何创建 NSBox 实例运行时并将其放置在 NSView 中?

Please, could you give me examples of how to create the NSBox instance runtime to place it inside the NSView?

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

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

发布评论

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

评论(1

大姐,你呐 2024-11-20 07:54:54
NSView *superview = …; // Reference to the view that will contain the box
                       // for instance, [window contentView]

NSUInteger resizeAllMask = (NSViewWidthSizable
    | NSViewHeightSizable
    | NSViewMinXMargin
    | NSViewMaxXMargin
    | NSViewMinYMargin
    | NSViewMaxYMargin);

// This is the box. We use an autoresizing mask so that it occupies
// the entire superview 
NSBox *box = [[NSBox alloc] initWithFrame:[superview bounds]];
[box setAutoresizingMask:resizeAllMask];
[box setBoxType:NSBoxPrimary];
[box setBorderType:NSBezelBorder];
[box setTitle:@"This is a box"];

// This is the box' content view. It represents the box contents.
// By default, a box autoresizes its content view so that it occupies
// the entire box
NSView *boxContentView = [[NSView alloc] initWithFrame:NSZeroRect];
[box setContentView:boxContentView];

// For example, we add a text field to the box' content view
NSRect textRect = {{0,0}, {100,20}};
NSTextField *textField = [[NSTextField alloc] initWithFrame:textRect];
[textField setStringValue:@"Hey there"];
[boxContentView addSubview:textField];

[superview addSubview:box];
NSView *superview = …; // Reference to the view that will contain the box
                       // for instance, [window contentView]

NSUInteger resizeAllMask = (NSViewWidthSizable
    | NSViewHeightSizable
    | NSViewMinXMargin
    | NSViewMaxXMargin
    | NSViewMinYMargin
    | NSViewMaxYMargin);

// This is the box. We use an autoresizing mask so that it occupies
// the entire superview 
NSBox *box = [[NSBox alloc] initWithFrame:[superview bounds]];
[box setAutoresizingMask:resizeAllMask];
[box setBoxType:NSBoxPrimary];
[box setBorderType:NSBezelBorder];
[box setTitle:@"This is a box"];

// This is the box' content view. It represents the box contents.
// By default, a box autoresizes its content view so that it occupies
// the entire box
NSView *boxContentView = [[NSView alloc] initWithFrame:NSZeroRect];
[box setContentView:boxContentView];

// For example, we add a text field to the box' content view
NSRect textRect = {{0,0}, {100,20}};
NSTextField *textField = [[NSTextField alloc] initWithFrame:textRect];
[textField setStringValue:@"Hey there"];
[boxContentView addSubview:textField];

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