Search code examples
iosobjective-clocalizationuisegmentedcontrol

UISegmentedControl and localized strings


I have a UISegmentedControl with 3 segments. The English strings fit perfectly, but Spanish strings are slightly longer and don't.

enter image description here

enter image description here

What is the correct procedure to manipulate font size dynamically for the string to fit the size of the segment? Is there something similar to the minimumFontScale property for UILabel?


Solution

  • Try This one You didn't need to set any min font size. It will change font size automatically to show title if needed.

    NSArray *itemArray = [NSArray arrayWithObjects: @"Title1 testing font size", @"Title2", @"Titl3",nil];
        UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:itemArray];
        segmentedControl.frame = YOUR_FRAME;
        [segmentedControl addTarget:self action:@selector(segmentControlClicked:) forControlEvents:UIControlEventValueChanged];
        [self.view addSubview:segmentedControl];
        for (id segment in [segmentedControl subviews])
        {
            for (id label in [segment subviews])
            {
                if ([label isKindOfClass:[UILabel class]])
                    [label setAdjustsFontSizeToFitWidth:YES];
    
            }           
        }