当我创建分段控件时,我使用以下代码:
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中的每个段都有两行文本,其中一行换行符合我的要求.
但是,在旋转时,我想保持分段控件的全宽,并且换行在宽段中看起来很傻.所以,在willAnimateRotationToInterfaceOrientation中,我包含了以下代码,字符串中没有换行符:
[_wheelDiameterSegmentedControl setFrame:CGRectMake(0,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行文本).
这是我遇到麻烦的地方.当我旋转回肖像时,我的选择似乎是:
1行标签文本,截断,格式
“实际:7 ……”
当我只是重置UISegmentedControl的大小时
[_wheelDiameterSegmentedControl setFrame:CGRectMake(0,50)];
2行标签文本,格式
“实际:
700c x ……“
当我重置大小并重置字符串值并重新运行将标签的numberOfLines设置为2的代码循环时,使用
NSArray *segmentedControlItemArray = [NSArray arrayWithObjects: @"Nominal:\n27 inch",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,50)];
“实际:
700c x
20毫米”
这是我用上面的强制numberOfLines = 2替换当我首先设置UISegmentedControl时使用的numberOfLines = 0时得到的.
我想要的是我在创建控件时得到的结果
“实际:
700c x 20mm“
但无论我尝试过什么(将字符串和numberOfLines代码放入willRotateToInterfaceOrientation或didRotateFromInterfaceOrientation;在更改文本之前重新设置UISegmentedControl的框架;在更改文本后重新设置框架……),我可以’得到我漂亮,整洁,两线的标签.我在这里想念的是什么?
解决方法
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,97,50); // //continuing as before titleLabel.numberOfLines = 0; } } }
现在它显示我想要/期望的完全.好哇!
如果您只是想在UISegmentedControl中获取两行(或更多行)文本.使用Herzian的惊人技术!这是另一个代码示例,可以帮助人们……
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; } }
再次感谢你.