How to change the font size of a segmented control and prevent it from changing to the default size after changing a segment

I use the following code to implement and then change the font size for each segment in 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 first (see screenshot below)

enter image description here

However, after I click on the โ€œpopularโ€ tab (inactive tab), the font size seems to return to the original default font size

enter image description here

What can I do to prevent the font from resizing to the default size?

+4
source share
2 answers

This is probably not the cleanest way, but it works if you run the for loop in the 'value Changed' event of the UISegmentedControl control.

Update: This is the correct method available in iOS 5 and later:

 NSDictionary *textAttributes = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont boldSystemFontOfSize:14], UITextAttributeFont, nil]; [self.segmentControl setTitleTextAttributes:textAttributes forState:UIControlStateNormal]; 
+9
source

http://chris-software.com/index.php/tag/uisegmentedcontrol/

check it out ans

 codeButton.segmentedControlStyle = UISegmentedControlStyleBar; codeButton.momentary = YES; 
+1
source

Source: https://habr.com/ru/post/1398914/


All Articles