Two lines of text in a UISegmentedControl

Try as I could, I can not solve the UISegmentedControl error for the iOS7 iPhone application.

When I create a 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]; 

Unfortunately, I cannot send images, or I would show you the image of exactly the control that I want: each of the segments in UISegmented Control has two lines of text with a line break where I requested it.

However, during rotation, I would like the segment control to be complete, and line breaks look silly in wide segments. So, in willAnimateRotationToInterfaceOrientation, I included the following code without line breaks in lines:
  [_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]; 

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

Here where I run into trouble. My choice, when I turn back to the portrait, is as follows:

1 line of label text, truncated, with format

"Actual: 7 ..."

when i just reset the size of the UISegmentedControl using

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

2 lines of label text, truncated, with format

"Actual:
700c x ... "

when i reset the size as well as reset the string values ​​and repeat the code loop that sets the numberOfLines label 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 lines of text labels with format

"Actual:
700c x
20mm "

what I get when I replace the forced number 0FLines = 2 above with the number OfLines = 0, which worked when I first set up the UISegmentedControl.

What I would like is what I get when I create a control that

"Actual:
700c x 20mm "

But no matter what I tried (by putting the string and numberOfLines code in willRotateToInterfaceOrientation or didRotateFromInterfaceOrientation, overriding the UISegmentedControl frame before changing the text, overriding the frame after changing the text ...) I can’t get my pretty, neat two-line label back. What am I missing here?

+7
ios uilabel uiinterfaceorientation uisegmentedcontrol
source share
3 answers

Yeah! Something about how the OS handles the size of a label when it is rotated is different from how it processes a label when it is created. So I forced the label frame size 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; } } } 

and now it is displayed exactly as I want / expect. Hooray!


If you are just trying to get two (or more) lines of text in your UISegmentedControl. Using Herzian's magnificent technology! Here is another example code that can 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; } } 

Thank you very much.

+7
source share

working code in ios 7:

I have a four-segment segment controller, and I need it in 2 lines for text. So I added UILabel as a subview in the UISegmentedControl segment, and it works well. I tried all other ways, but on ios 7 and ios 8 it works well right now.

 for(int k =0;k<4;k++) { NSString *text = @"title name"; UILabel *label = [[UILabel alloc] init]; label.frame = CGRectMake(10,2, (self.segmentcontroll.frame.size.width/4), 34); label.font = [UIFont fontWithName:@"HelveticaNeue" size:12.0]; label.textColor = [UIColor whiteColor]; label.text = text; label.numberOfLines = 0; label.textAlignment = NSTextAlignmentCenter; [label sizeToFit]; [[[self.segmentcontroll subviews] objectAtIndex:k] addSubview:label]; } 
+1
source share

And Herzian’s decision as a category, if someone wants it. Remember to call it in VWA instead of VDL, so this happens after auto-negotiation / rotation

 #import "UISegmentedControl+MultiLine.h" @implementation UISegmentedControl (MultiLine) -(void)setupMultiLine { self.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y, self.frame.size.width, self.frame.size.height*1.6); for (id segment in self.subviews) { for (id label in [segment subviews]) { if ([label isKindOfClass:[UILabel class]]) { UILabel *titleLabel = (UILabel*) label; titleLabel.frame = CGRectMake(0, 0, titleLabel.frame.size.width, titleLabel.frame.size.height*1.6); titleLabel.numberOfLines = 0; } } } } @end 
0
source share

All Articles