Is it possible to create multi line UISegmentedControl?

后端 未结 5 428
不知归路
不知归路 2020-12-11 05:21

I have a relative longer text items in my segmented control so I need to break text at certain points. Is it possible to use line breaks? I know at buttons

相关标签:
5条回答
  • 2020-12-11 05:56

    I changed little bit the code from this link, and here is Swift extension:

    extension UISegmentedControl {
    
      func makeTitleMultiline(){
        for i in 0...self.numberOfSegments - 1 {
          let label = UILabel(frame: CGRect(x: 0, y: -7, width: (self.frame.width-10)/CGFloat(self.numberOfSegments), height: self.frame.height))
          label.textColor = i == 0 ? UIColor.red : UIColor.blue
          label.text = self.titleForSegment(at: i)
          label.numberOfLines = 0
          label.textAlignment = .center
          label.adjustsFontSizeToFitWidth = true
          label.tag = i
          self.setTitle("", forSegmentAt: i)
          self.subviews[i].addSubview(label)
        }
      }
    
      func setSelectedTitleColor() {
        for i in 0...self.numberOfSegments - 1 {
          let label = self.subviews[self.numberOfSegments - 1 - i].subviews[1] as? UILabel
          label?.textColor = label?.tag == self.selectedSegmentIndex ? UIColor.red : UIColor.blue
        }
      }
    }
    
    0 讨论(0)
  • 2020-12-11 05:57

    Please refer to this answer

    Swift 3+

    UILabel.appearance(whenContainedInInstancesOf: [UISegmentedControl.self]).numberOfLines = 0
    

    Objective C

    [[UILabel appearanceWhenContainedIn:[UISegmentedControl class], nil] setNumberOfLines:0];
    
    0 讨论(0)
  • 2020-12-11 05:58

    Swift 4 version of solution. segmentedControl is your instance of UISegmentedControl.

            for segmentItem : UIView in segmentedControl.subviews
        {
            for item : Any in segmentItem.subviews {
                if let i = item as? UILabel {
                    i.numberOfLines = 0
                    // change other parameters: color, font, height ... 
                }
            }
        }
    

    Don't forget to set segmentedControl's height as a doubled font's height.

    0 讨论(0)
  • 2020-12-11 06:00

    if you have a standard UISegmentedControl you can use the following idea:

    [_segmentedControl.subviews enumerateObjectsUsingBlock:^(UIView * obj, NSUInteger idx, BOOL *stop) {
        [obj.subviews enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
            if ([obj isKindOfClass:[UILabel class]]) {
                UILabel *_tempLabel = (UILabel *)obj;
                [_tempLabel setNumberOfLines:0];
            }
        }];     
    }];
    

    you may need to set the height of your instance as well.


    NOTE: I need to add a little warning about – as rmaddy has also pointed out correctly – this is a quite fragile solution, because if the segmented control's view hierarchy would be changed in the future iOS versions that code may not work properly anymore.

    0 讨论(0)
  • 2020-12-11 06:12

    Add [yourSegmentedControl layoutIfNeeded]; method, before adding it as a subview. Then iterate all the SegmentedControl subviews and modify the numberOfLines property of the title label.

    Here is the code

    for (id segmentItem in [yourSegmentedControl subviews])
    {
      if ([segmentItem isKindOfClass:[UILabel class]])
       {
         UILabel *titleLabel = (UILabel *) segmentItem;
         titleLabel.frame = CGRectMake(0, 0, 100, 50); //modify the frame if the second line is not showing.
         titleLabel.numberOfLines = 0;
       }
    }
    
    0 讨论(0)
提交回复
热议问题