IOS - Change the text font for each segment in the UISegmentController

I got UISegmentedControlcalled mySegmentedControl

@property (nonatomic) IBOutlet UISegmentedControl *mySegmentedControl;

with 3 segments, and I would change the font of the text for each segment. Maybe?

+4
source share
5 answers

Yes you can ----- Try this

for(uint i=0;i<[mySegmentedControl subviews].count;i++)
{
    for(UIView *view in [[[mySegmentedControl subviews] objectAtIndex:i] subviews])
    {
        if([view isKindOfClass:[UILabel class]])
        {
            if(i==0)  // set First segment font
                [(UILabel*)view setFont:[UIFont fontWithName:@"HelveticaNeue-Light" size:27]];

            if(i==1)  // set Second segment font
                [(UILabel*)view setFont:[UIFont fontWithName:@"HelveticaNeue-Light" size:17]];

            if(i==2)  // set Third segment font
                [(UILabel*)view setFont:[UIFont fontWithName:@"HelveticaNeue-Light" size:7]];
        }
    }
}
+1
source

I do not think that's possible. Your options are either to use a custom open source implementation, or to set a custom image with text for each segment.

0
source

. .

NSArray *segemtImages = [NSArray arrayWithObjects:[UIImage imageNamed:@"1st.png"],[UIImage imageNamed:@"2nd.png"],[UIImage imageNamed:@"3rd.png"], nil];
UISegmentedControl *segmentControl = [[UISegmentedControl alloc] initWithItems:segemtImages];
0

I am changing @Anand Natan code: In the storyboard, set the first label to "1111111111111" to change its default width. Because changing the label does not change the size of the segmentation.

    for(uint i=0;i<[_menuBarTop subviews].count;i++){
        for(uint j=0;j<[[[[_menuBarTop subviews] objectAtIndex:i] subviews]count];j++){
            UIView* view = [[[_menuBarTop subviews] objectAtIndex:i] subviews][j];
            if([view isKindOfClass:[UILabel class]]){
                UILabel* label = (UILabel*)view;
                if([label.text isEqualToString:@"1111111111111"]){
                    label.text = LANGLOC(@"videolist_category_choosen");
                }else if([label.text isEqualToString:@"2"]){
                    label.text = LANGLOC(@"videolist_category_followed");
                }
                break;
            }
        }
    }
0
source
NSDictionary *textAttributes = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont boldSystemFontOfSize:12], NSFontAttributeName, nil];
[_sgmntStatusUI setTitleTextAttributes:textAttributes forState:UIControlStateNormal];

for ios7 later varsions

-1
source

All Articles