有没有办法以编程方式确定适当的大小为苹果的内置控件? [英] Is there a way to programmatically determine the proper sizes for Apple's built-in controls?

查看:90
本文介绍了有没有办法以编程方式确定适当的大小为苹果的内置控件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编写Cocoa应用程序时,我以编程方式完成大部分用户界面布局。例如:

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];
//...

我的问题是关于 kDefaultPopUpButtonHeight 常数。我目前维护一个充满这样的常量的源文件,我手动填充适当的大小。我可以通过在Interface Builder中的一个空白视图中放置一个新控件,然后检查其属性来确定IB的大小来确定正确的大小。

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.

是一个更好的方式。是否可以在运行时访问这些值?理想情况下,我希望每个 NSControl 有一个类方法: +(NSSize)defaultSize 控件类似于 NSButton ,它们具有不同的默认大小,具体取决于所使用的特定按钮样式,例如 +(NSSize)defaultSizeForButtonStyle:(NSButtonStyle)buttonStyle

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的人机界面指南包含有关控件布局和控件之间的间距的信息,但它并未说明有关各个控件的正确大小的信息。

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.

推荐答案

我同意Peter,并建议您使用Interface Builder。但是如果这在你的情况下是不合适的,这里有一种方法来找到大多数控件的最佳尺寸:

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:

NSSize idealSize = [[control cell] cellSize];

如果您需要更多地控制大小,可以使用 - [NSCell cellSizeForBounds:]方法。

If you need more control over the sizing, you can use the -[NSCell cellSizeForBounds:] method.

此外,cellSize真的给你一个控件的最小尺寸,不一定是最好的尺寸。例如,对于带有文本OK的Cocoa aqua样式按钮,它将返回比HIG将推荐的宽度更窄的宽度。为了你的目的,它听起来像你只对固定高度部分的大小感兴趣。 - [NSCell cellSize]应该工作很好。

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.

这篇关于有没有办法以编程方式确定适当的大小为苹果的内置控件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆