有没有办法以编程方式确定 Apple 内置控件的正确尺寸?
在编写 Cocoa 应用程序时,我以编程方式完成大部分用户界面布局。 例如:
NSRect popUpFrame = NSMakeRect(10, 10, 100, kDefaultPopUpButtonHeight);
NSPopUpButton * popUp = [[NSPopUpButton alloc] initWithFrame:popUpFrame];
//...
我的问题是关于 kDefaultPopUpButtonHeight 常量的。 我目前维护一个充满此类常量的源文件,并手动填写适当的大小。 我可以通过将新控件放入 Interface Builder 中的空白视图中,然后检查其属性以查看 IB 为其提供的大小来确定正确的大小。
一定会有更好的办法。 是否可以在运行时访问这些值? 理想情况下,我希望每个 NSControl
都有一个类方法,例如:+(NSSize)defaultSize
,或者对于像 NSButton
这样的控件不同的默认大小取决于所使用的特定按钮样式,例如 +(NSSize)defaultSizeForButtonStyle:(NSButtonStyle)buttonStyle
。
Apple 的人机界面指南 包含有关控件布局和控件之间的间距,但它没有说明各个控件的正确大小。
When writing Cocoa apps, I do the majority of the user interface layout programmatically. For example:
NSRect popUpFrame = NSMakeRect(10, 10, 100, kDefaultPopUpButtonHeight);
NSPopUpButton * popUp = [[NSPopUpButton alloc] initWithFrame:popUpFrame];
//...
My question is about that kDefaultPopUpButtonHeight
constant. I currently maintain a source file full of such constants, and I fill in the proper sizes manually. I am able to determine the correct sizes by dropping a new control into a blank view in Interface Builder and then checking its properties to see what size IB gives it.
There must be a better way. Is it possible to access these values at runtime? Ideally, I would expect every NSControl
to have a class method something like: +(NSSize)defaultSize
, or, for controls like NSButton
that have different default sizes depending on the particular button style used, something like +(NSSize)defaultSizeForButtonStyle:(NSButtonStyle)buttonStyle
.
Apple's Human Interface Guidelines has information about control layout and the spacing between controls, but it doesn't say anything about the proper sizes for individual controls.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我同意 Peter 的观点,并建议您使用 Interface Builder。 但如果这不适合您的情况,这里有一种方法可以为大多数控件找到最佳大小:
如果您需要对大小进行更多控制,可以使用 -[NSCell cellSizeForBounds:] 方法。
此外,cellSize 实际上为您提供了控件的最小尺寸,而不一定是最佳尺寸。 例如,对于带有文本“OK”的 Cocoa aqua 样式按钮,它将返回比 HIG 建议的宽度更窄的宽度。 出于您的目的,听起来您只对尺寸的固定高度部分感兴趣。 -[NSCell cellSize] 应该很好用。
I agree with Peter, and would recomend that you use Interface Builder. But if that isn't appropriate in your situation, here's one way to find the best size for most controls:
If you need more control over the sizing, you can use the -[NSCell cellSizeForBounds:] method.
Also, cellSize really gives you the minimum size for a control, not necessarily the best size. For example, for a Cocoa aqua style push button with the text "OK", it would return a width that more narrow than the HIG would recommend. For your purposes, it sounds like you're only interested in the fixed hight portion of the size. -[NSCell cellSize] should work great.