I am using the following code to implement and subsequently change the font size of each segment in the UISegmented Control
//Set up segment control
UISegmentedControl *tempSegmentControl = [[UISegmentedControl alloc]initWithItems:[NSArray arrayWithObjects:@"Friends",@"Popular", nil]];
tempSegmentControl.frame = CGRectMake(-8, -1, 336, 30);
self.segmentControl = tempSegmentControl;
[self.segmentControl setWidth:168 forSegmentAtIndex:0];
[self.segmentControl setWidth:168 forSegmentAtIndex:1];
self.segmentControl.selectedSegmentIndex = 0;
[self.segmentControl addTarget:self action:@selector(toggleControls:) forControlEvents:UIControlEventValueChanged];
[self.segmentControl setSegmentedControlStyle:UISegmentedControlStylePlain];
//TO CHANGE FONT SIZE OF EACH SEGMENT
for (id segment in [self.segmentControl subviews])
{
for (id label in [segment subviews])
{
if ([label isKindOfClass:[UILabel class]])
{
[label setTextAlignment:UITextAlignmentCenter];
[label setFont:[UIFont boldSystemFontOfSize:14]];
}
}
}
This works initially (see screenshot below)
However, after I click on the "popular" tab (inactive tab), the font size seem to return to their original default font size
What can I do to prevent the font size from changing back to the default size?
Probably not the cleanest way, but it works if you run the for-loop on the 'value Changed' event of the UISegmentedControl control.
Update: This is the proper way, available in iOS 5 and later:
NSDictionary *textAttributes = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont boldSystemFontOfSize:14], UITextAttributeFont, nil];
[self.segmentControl setTitleTextAttributes:textAttributes forState:UIControlStateNormal];
http://chris-software.com/index.php/tag/uisegmentedcontrol/
check out this its ans
codeButton.segmentedControlStyle = UISegmentedControlStyleBar;
codeButton.momentary = YES;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With