简体   繁体   中英

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.

在此输入图像描述

在此输入图像描述

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 ?

try this one

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

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

        }           
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM