How to change font color of UISegmentedControl

I am trying to change the font color from white to black for UISegmentedControl (for iOS 4. *)

 UISegmentedControl *button = [[[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:itemTitle, nil]] autorelease]; button.momentary = YES; button.segmentedControlStyle = UISegmentedControlStyleBar; button.tintColor = [UIColor redColor]; for (id segment in [button subviews]) { for (id label in [segment subviews]) { if ([label isKindOfClass:[UILabel class]]) { UILabel *titleLabel = (UILabel *) label; [titleLabel setTextColor:[UIColor blackColor]]; } } } UIBarButtonItem *barButtonItem = [[[UIBarButtonItem alloc] initWithCustomView:button] autorelease]; 

But the color of the text does not change. What should I do to change the text color for a UISegmentedControl ?

+58
objective-c iphone cocoa-touch uisegmentedcontrol
Jan 27 '12 at 6:32
source share
10 answers

In iOS 6.0 and above, this is very simple:

 NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys: [UIFont boldSystemFontOfSize:17], NSFontAttributeName, [UIColor blackColor], NSForegroundColorAttributeName, nil]; [_segmentedControl setTitleTextAttributes:attributes forState:UIControlStateNormal]; NSDictionary *highlightedAttributes = [NSDictionary dictionaryWithObject:[UIColor whiteColor] forKey:NSForegroundColorAttributeName]; [_segmentedControl setTitleTextAttributes:highlightedAttributes forState:UIControlStateSelected]; 
+118
Nov 18 '12 at 10:19
source share

If you need to change the text color of the selected segment in iOS 7, here is the solution (it took me a while to find, but thanks to this post )

Objective-c

 [[UISegmentedControl appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor blackColor]} forState:UIControlStateSelected]; 

Swift

  let titleTextAttributes = [NSForegroundColorAttributeName: UIColor.whiteColor()] UISegmentedControl.appearance().setTitleTextAttributes(titleTextAttributes, forState: .Selected) 
+67
Jan 31 '14 at 16:17
source share

Below is the code to set the font to bold face and dot size:

 UIFont *Boldfont = [UIFont boldSystemFontOfSize:12.0f]; NSDictionary *attributes = [NSDictionary dictionaryWithObject:Boldfont forKey: NSFontAttributeName]; [segmentedControl setTitleTextAttributes:attributes forState:UIControlStateNormal]; 

I hope this helps

+29
Jan 27 '12 at 12:52
source share

Swift 4 code to set the font color to black in both states

  let titleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.black] segmentedControl.setTitleTextAttributes(titleTextAttributes, for: .normal) segmentedControl.setTitleTextAttributes(titleTextAttributes, for: .selected) 
+17
Jan 23 '18 at 16:03
source share
 for (UIView *v in [[[segment subviews] objectAtIndex:0] subviews]) { if ([v isKindOfClass:[UILabel class]]) { UILabel *label=(UILabel *)[v retain]; lable.textColor=[UIColor blackColor]; } } 

for iOS 3.0 and higher

+6
Feb 18 '12 at 9:21
source share

Updated for Swift 4 - use this extension (because the extension is always awesome .. !!)

 extension UISegmentedControl { func defaultConfiguration(font: UIFont = UIFont.systemFont(ofSize: 12), color: UIColor = UIColor.gray) { let defaultAttributes = [ NSAttributedStringKey.font.rawValue: font, NSAttributedStringKey.foregroundColor.rawValue: color ] setTitleTextAttributes(defaultAttributes, for: .normal) } func selectedConfiguration(font: UIFont = UIFont.boldSystemFont(ofSize: 12), color: UIColor = UIColor.red) { let selectedAttributes = [ NSAttributedStringKey.font.rawValue: font, NSAttributedStringKey.foregroundColor.rawValue: color ] setTitleTextAttributes(selectedAttributes, for: .selected) } } 

and from the corresponding class, you can use these functions,

 @IBOutlet weak var segment: UISegmentedControl! segment.defaultConfiguration() // or provide paramater as per your requirement segment.selectedConfiguration(color: .blue) 
+5
Jun 11 '18 at 10:14
source share

Just in case, to help someone else who gets there and works with quick access.

I will put two possible ways to do this. You can change the text attributes in UIAppearance or directly in the segmented one that you work with.

The first example defines appearance attributes, so you configure all the segmented controls in your application:

  let attributes = [ NSForegroundColorAttributeName : UIColor.grayColor(), NSFontAttributeName : UIFont.systemFontOfSize(20)]; let attributesSelected = [ NSForegroundColorAttributeName : UIColor.blueColor(), NSFontAttributeName : UIFont.systemFontOfSize(20)]; UISegmentedControl.appearance().setTitleTextAttributes(attributes, forState: UIControlState.Normal) UISegmentedControl.appearance().setTitleTextAttributes(attributesSelected, forState: UIControlState.Selected) 

The second example, directly in a segmented one, will configure only this segmented one:

  let attributes = [ NSForegroundColorAttributeName : UIColor.grayColor(), NSFontAttributeName : UIFont.systemFontOfSize(20)]; let attributesSelected = [ NSForegroundColorAttributeName : UIColor.blueColor(), NSFontAttributeName : UIFont.systemFontOfSize(20)]; segmented.setTitleTextAttributes(attributes, forState: UIControlState.Normal) segmented.setTitleTextAttributes(attributesSelected, forState: UIControlState.Selected) 
+4
Apr 27 '16 at 0:45
source share

swift 3.2:

 let attributes = [ NSFontAttributeName : bigTextFont, NSForegroundColorAttributeName : UIColor.blue, ] segmentedControl?.setTitleTextAttributes(attributes, for: .normal) 

Note: if you use a custom background color, you will see a glitch in the corners (the color will fill the outer segments ..), so use this line to copy it:

 segmentedControl!.layer.cornerRadius = 4.0 segmentedControl!.clipsToBounds = true 
+4
Aug 03 '17 at 12:17
source share

In iOS 5.0 and later, you can use titleTextAttributes to configure UISegmentedControl objects:

 NSDictionary *segmentedControlTextAttributes = @{NSFontAttributeName:[UIFont fontWithName:@"HelveticaNeue" size:18.0], NSForegroundColorAttributeName:[UIColor whiteColor]}; [self.segmentedControl setTitleTextAttributes:segmentedControlTextAttributes forState:UIControlStateNormal]; [self.segmentedControl setTitleTextAttributes:segmentedControlTextAttributes forState:UIControlStateHighlighted]; [self.segmentedControl setTitleTextAttributes:segmentedControlTextAttributes forState:UIControlStateSelected]; 

Here I set the font for the custom font, font size, color for each state of the UISegmentedControl .

You will find all sorts of simple settings in the Customizing the Appearance section of the UISegmentedControl Class Reference.

+2
Oct 17 '14 at 9:34
source share

Im using monotouch. I don’t know why, but when the View was clicked, the color of the text did not change for me. To solve this problem, I simply add labels to segment the supervision of the supervision, and then change their colors:

 public static void SetColoredTittles(this UISegmentedControl s, string[] titles, UIColor selected, UIColor notSelected) { var segmentedLabels = new List<UILabel>(); float width = s.Frame.Width/titles.Length; for (int i = 0; i < titles.Length; i++) { var frame = new RectangleF(s.Frame.X + i*width, s.Frame.Y, width,s.Frame.Height); UILabel label = new UILabel(frame); label.TextAlignment = UITextAlignment.Center; label.BackgroundColor = UIColor.Clear; label.Font = UIFont.BoldSystemFontOfSize(12f); label.Text = titles[i]; s.Superview.AddSubview(label); segmentedLabels.Add(label); } s.ValueChanged += delegate { TextColorChange(s,segmentedLabels, selected, notSelected); }; TextColorChange(s,segmentedLabels, selected, notSelected); } static void TextColorChange(UISegmentedControl s, List<UILabel> segmentedLabels, UIColor selected, UIColor notSelected) { for (int i = 0; i < segmentedLabels.Count; i++) { if(i == s.SelectedSegment) segmentedLabels[i].TextColor = selected; else segmentedLabels[i].TextColor = notSelected; } } 

and then use it

 segmented.SetColoredTittles(new string[] { "text1", "text2", "text3" }, UIColor.White,UIColor.DarkGray); 
+1
Nov 16 '12 at 18:11
source share



All Articles