如何计算UISegmentedControl段的正确宽度? [英] How can I calculate correct widths for UISegmentedControl segments?

查看:419
本文介绍了如何计算UISegmentedControl段的正确宽度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用UISegmentedControl,但无法计算段的宽度。该控件使得段的宽度相同,但对于某些标题不起作用,例如:

I'm trying to use a UISegmentedControl but having trouble calculating the width of the segments. The control makes the segments all the same width which doesn't work for some titles, like this one:

http://morrisphotoart.com/tmp/Scr​​eenshot2011-07-13_21.17.33.png

如果我知道哪种字体并调用setWidth:forSegmentAtIndex:方法,我可以编写代码来计算段宽,但是如何获取字体呢?或者还有另一种方式吗?

I can write code to calculate the segment width if I knew which font and call the setWidth:forSegmentAtIndex: method but how can I get the font? Or is there another way?

左侧和中间段的标题不固定,因此我无法对宽度进行硬编码。

The left and middle segment's titles are not fixed so I can't hardcode the widths.

推荐答案

如果您可以支持iOS 5及更高版本,那么您可以使用属性 apportionsSegmentWidthsByContent 并将其设置为 YES

If you can support iOS 5 and later then you can use the property apportionsSegmentWidthsByContent and set it to YES.

来自iOS 5 docs


apportionsSegmentWidthsByContent

指示控件
是否尝试根据内容宽度调整分段宽度。

Indicates whether the control attempts to adjust segment widths based on their content widths.

@property(nonatomic)BOOL apportionsSegmentWidthsByContent

@property(nonatomic) BOOL apportionsSegmentWidthsByContent

讨论

如果此属性的值为YES,则对于细分其宽度值
为0,控件尝试根据
调整分段宽度内容宽度。

If the value of this property is YES, for segments whose width value is 0, the control attempts to adjust segment widths based on their content widths.

这篇关于如何计算UISegmentedControl段的正确宽度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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