Two lines of text in a UISegmentedControl

后端 未结 3 1038
日久生厌
日久生厌 2020-12-15 00:34

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 *segmen         


        
相关标签:
3条回答
  • 2020-12-15 01:06

    And Herzian's solution as a category if anyone wants it. Remember to call it in VWA instead of VDL so it happens after autosizing / 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 讨论(0)
  • 2020-12-15 01:19

    working code in ios 7 :

    I have a segment controller with four segments and I need it in 2 lines for text. So I have added UILabel as a subview in segment of UISegmentedControl and it is working good. I have tried all other ways but in ios 7 and ios 8 it is working good 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];
    }
    
    0 讨论(0)
  • 2020-12-15 01:24

    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;
    
            }
        }
    }
    

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


    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;
                }
            }
    

    thank you so much, again.

    0 讨论(0)
提交回复
热议问题