UISegmentedControl中的两行文本

问题描述:

尽我所能,我无法解决iOS7 iPhone应用程序的UISegmentedControl错误。

Try as I might, I can't solve a UISegmentedControl bug for an iOS7 iPhone app.

当我创建分段控件时,我使用此代码:

When I create the segmented control, I use this code:

NSArray *segmentedControlItemArray = [NSArray arrayWithObjects: @"Nominal:\n27 inch", @"Actual:\n700c x 23mm", @"Actual:\n700c x 20mm", nil];
_wheelDiameterSegmentedControl = [[UISegmentedControl alloc] initWithItems:segmentedControlItemArray];
_wheelDiameterSegmentedControl.frame = CGRectMake(0, 102, 290, 50);
_wheelDiameterSegmentedControl.selectedSegmentIndex = 0;
_wheelDiameterSegmentedControl.tintColor = [UIColor colorWithRed:0.35 green:0.4 blue:0.9 alpha:1.0];

for (id segment in [_wheelDiameterSegmentedControl subviews]) {
    for (id label in [segment subviews]) {
        if ([label isKindOfClass:[UILabel class]]) {
            UILabel *titleLabel = (UILabel *) label;
            titleLabel.numberOfLines = 0;   
        }   
    }  
}

[_wheelDiameterSegmentedControl addTarget:self
                     action:@selector(pickOne:)
           forControlEvents:UIControlEventValueChanged];

[_wheelDiameterMenuContainer addSubview:_wheelDiameterSegmentedControl];

可悲的是,我无法发布图片,或者我会向您展示完全控制的图片我想要:UISegmented Control中的每个段都有两行文本,其中的换行符合我要求的位置。

Sadly, I can't post images, or I would show you a picture of exactly the control I want: each of the segments in the UISegmented Control has two lines of text, with a line break exactly where I asked for it.

在旋转时,我会喜欢保持分段控件的全宽度,并且换行在宽阔的段中看起来很傻。所以,在willAnimateRotationToInterfaceOrientation中,我已经包含了以下代码,字符串中没有换行符:

On rotation, though, I'd like to keep the segmented control full-width, and the line breaks look silly in segments that wide. So, in willAnimateRotationToInterfaceOrientation, I've included the following code, with no line breaks in the strings:

    [_wheelDiameterSegmentedControl setFrame:CGRectMake(0, 102, 450, 50)];
    [_wheelDiameterSegmentedControl setTitle:@"Nominal: 27 inch" forSegmentAtIndex:0];
    [_wheelDiameterSegmentedControl setTitle:@"Actual: 700c x 23mm" forSegmentAtIndex:1];
    [_wheelDiameterSegmentedControl setTitle:@"Actual: 700c x 20mm" forSegmentAtIndex:2];

再一次,如果我可以插入图片,我会告诉你一幅图片想要:广泛的UISegmented控件,标签中没有换行符(每个标签1行文本)。

And once again, if I could insert an image, I would show you an image of exactly what I want: a wide UISegmented Control with no line breaks in the labels (1 line of text per label).

这是我遇到麻烦的地方。当我旋转回肖像时,我的选择似乎是:

Here's where I run in to trouble. My choices, when I rotate back to portrait, seem to be:

1行标签文本,截断,格式为

1 line of label text, truncated, with the format

实际:7 ...

当我只使用

[_wheelDiameterSegmentedControl setFrame:CGRectMake(0, 102, 290, 50)];

2行标签文本,截断,格式为

2 lines of label text, truncated, with the format

实际:

700c x ...

"Actual:
700c x ..."

当我重置大小并重置字符串值并重新运行将标签的numberOfLines设置为2的代码循环,使用

when I reset the size and also reset the string values and rerun the loop of code that sets the label's numberOfLines to 2, using

    NSArray *segmentedControlItemArray = [NSArray arrayWithObjects: @"Nominal:\n27 inch", @"Actual:\n700c x 23mm", @"Actual:\n700c x 20mm", nil];

    [_wheelDiameterSegmentedControl setTitle:[segmentedControlItemArray objectAtIndex:0] forSegmentAtIndex:0];
    [_wheelDiameterSegmentedControl setTitle:[segmentedControlItemArray objectAtIndex:1] forSegmentAtIndex:1];
    [_wheelDiameterSegmentedControl setTitle:[segmentedControlItemArray objectAtIndex:2] forSegmentAtIndex:2];
    for (id segment in [_wheelDiameterSegmentedControl subviews]) {
        for (id label in [segment subviews]) {
            if ([label isKindOfClass:[UILabel class]]) {
                UILabel *titleLabel = (UILabel *) label;
                titleLabel.numberOfLines = 2;
            }
        }
    }
    [_wheelDiameterSegmentedControl setFrame:CGRectMake(0, 102, 290, 50)];

3行标签文字,格式为

3 lines of label text, with the format

实际:

700c x

20mm

"Actual:
700c x
20mm"

这是我更换强制numberOfLines时得到的当我在第一时间设置UISegmentedControl时,numberOfLines = 0时,= 2以上。

which is what I get when I replace the forced numberOfLines = 2 above with the numberOfLines = 0 that worked when I set up the UISegmentedControl in the first place.

创建控件时我得到的是

实际:

700c x 20mm

"Actual:
700c x 20mm"

但无论我尝试过什么(将字符串和numberOfLines代码放入willRotateToInterfaceOrientation或didRotateFromInterfaceOrientation;重新设置更改文本之前UISegmentedControl的框架;更改文本后重新设置框架...),我无法得到我漂亮,整洁,双线标签。我在这里想念的是什么?

But no matter what I've tried (putting the string and numberOfLines code into willRotateToInterfaceOrientation or didRotateFromInterfaceOrientation; re-setting the frame of the UISegmentedControl before changing the text; re-setting the frame after changing the text...), I can't get my nice, neat, two-line label back. What am I missing here?

啊哈!操作系统在旋转时如何处理标签大小的方法与标签在创建时的处理方式不同。所以我强制标签框架的大小,使用

Aha! Something about how the OS is handling the size of the label when it rotates is different from how it handles the label when it's created. So I forced the size of the frame of the label, using

for (id segment in [_wheelDiameterSegmentedControl subviews]) {

    for (id label in [segment subviews]) {

        if ([label isKindOfClass:[UILabel class]]) {

            UILabel *titleLabel = (UILabel *) label;

            //inserting line here, to make the frame behave nicely:
            //
            titleLabel.frame = CGRectMake(0, 0, 97, 50);
            //
            //continuing as before

            titleLabel.numberOfLines = 0;

        }
    }
}

现在它显示我想要/期望的完全。 Huzzah!

and now it displays exactly as I want/expect. Huzzah!

如果您只想在UISegmentedControl中获取两行(或更多行)文本。使用Herzian的惊人技术!这里只是另一个代码示例,可以帮助人们......

If you're just trying to get two (or more) lines of text in your UISegmentedControl. Using Herzian's amazing technology! Here's just another code example, that may help people...

self.yourSlider .. set the height manually (can't easily do it in XIB)
// now use amazing Herzian technology...
for (id segment in [self.yourSlider subviews])
    for (id label in [segment subviews])
        {
        if ([label isKindOfClass:[UILabel class]])
            {
            UILabel *titleLabel = (UILabel *) label;
            titleLabel .. set the width to say 80, per your layout;
            (the width us typically too low)
            titleLabel.numberOfLines = 0;
            }
        }

再次非常感谢你。