Two lines of text in a UISegmentedControl

痞子三分冷 提交于 2019-11-28 22:00:23
herzian

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.

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

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
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!