UISegmentedControl and localized strings

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

enter image description here

enter image description here

What is the correct procedure to dynamically change the font size to make the line fit the segment size? Something similar to a property minimumFontScale

for UILabel

?

+3


source to share


2 answers


Try this one. You didn't need to set the minimum font size. It will automatically resize the font to show the heading 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];

        }           
    }

      

+1


source


try this



[_segmentedControl.subviews enumerateObjectsUsingBlock:^(UIView * obj, NSUInteger idx, BOOL *stop) {
    [obj.subviews enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
        if ([obj isKindOfClass:[UILabel class]]) {
            UILabel *lblCaption = (UILabel *)obj;
            lblCaption.minimumFontSize=10.0f;
//OR
            lblCaption.minimumFontSize = 0.5f;
        }
    }];     
}];

      

+1


source







All Articles