14

Iam new to objective c,need to change the text color of selected segment in UIsegmentControl. Used following code.

 [[UIsegmentControl.subviews objectAtIndex:segment.selectedSegmentIndex] setTintColor:[UIColor redColor]];

it changes segment color.Help me please..

1

7 Answers 7

41

There's no way to set the custom color of selected segment title in UISegmentedControl. The UIControlState in forState: used to set the attributes of segment text for normal and selected state.

From Your Code :

[[UIsegmentControl.subviews objectAtIndex:segment.selectedSegmentIndex] setTintColor:[UIColor redColor]];

Try This Code:

[segmnt_cntrl setTitleTextAttributes:@{NSFontAttributeName:[UIFont fontWithName:@"Arial" size:16.0],
                                                          NSForegroundColorAttributeName:[UIColor redColor],
                                                          NSShadowAttributeName:shadow}
                                                         forState:UIControlStateNormal];

Replace the segmnt_cntrl with your object of Segment Cotrol. Try this , It might helps you to achieve your over all goal.

Thanks

3
  • 3
    This answer is correct except for your first paragraph. The UIControlState in forState: can be used to set the attributes for normal and selected segment text.
    – jrturton
    Oct 27, 2012 at 7:52
  • What about attribute's range? I want to have two different colors in one title. Is it possible with standard UISegmentedControl?
    – Piotr
    Nov 28, 2013 at 11:15
  • hi. WHat is NSShadowAttributeName: shadow? I can find shadow name attribute
    – kemdo
    Oct 5, 2017 at 6:19
21

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

[[UISegmentedControl appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor colorBlack]} forState:UIControlStateSelected];
2
  • can you please tell me what to do if I want to change the text color of the highlighted as well as the normal segment to black. Right now it is blue in color Mar 26, 2014 at 11:59
  • @NehaDangui, it would be a matter of replacing UIControlStateSelected with UIControlStateNormal (or UIControlStateHighlighted if you need to).
    – i--
    Mar 26, 2014 at 13:17
13

With references to @i-- Answer

Updated Swift 5

UISegmentedControl.appearance().setTitleTextAttributes([NSAttributedString.Key.foregroundColor: UIColor.red], for: .selected)

Updated Swift 4.1

UISegmentedControl.appearance().setTitleTextAttributes([NSAttributedStringKey.foregroundColor: UIColor.red], for: .selected)

Swift 3.1

UISegmentedControl.appearance().setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.red as Any], for: .selected)

For Earlier Swift Version:

UISegmentedControl.appearance().setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.red], for: .selected)
1
  • Swift 5: UISegmentedControl.appearance().setTitleTextAttributes([NSAttributedString.Key.foregroundColor: UIColor.white], for: .selected)
    – Sravan
    Apr 7, 2019 at 19:46
4

In addition to @Babl Prabhakar's answer

Swift 5: UISegmentedControl.appearance().setTitleTextAttributes([NSAttributedString.Key.foregroundColor: UIColor.white], for: .selected)

2

There is no standard API to set the text attributes of a single segment in a UISegmentedControl. You can do the unrecommended approach of digging into the view hierarchy of the segmented control, find the desired UILabel (if any) and set that label's attributes. A better approach is to find (or write) a custom control that emulates a UISegmentedControl and allows for a single segment to be customized in the way you need.

Edit:

Actually, I was looking at this from the wrong point of view. My answer was based on trying to set attributes for a specific segment index. But in fact this can be achieved by setting the text attributes for the UIControlStateSelected state. Sorry for the confusion.

2
  • You can set text attributes using the UIAppearance methods, for a specific control state. So this answer is wrong.
    – jrturton
    Oct 27, 2012 at 7:51
  • @jrturton Of course. My mistake. I was thinking about this from the point of view of segment index. But in this case it can be done for the highlighted state. I'll update my answer. Thanks
    – rmaddy
    Oct 27, 2012 at 15:50
1

Objective C

set font style, size and color

NSDictionary *attributes = [NSDictionary dictionaryWithObject: [UIFont fontWithName: @"Arial" size:16] forKey:NSFontAttributeName];
[self->mySegmentControl setTitleTextAttributes: attributes forState: normal];
[self->mySegmentControl setTitleTextAttributes: @{NSForegroundColorAttributeName: UIColor.blueColor} forState: UIControlStateSelected];
0

Updated @Babul Prabhakar's answer for Swift 3, because about half a dozen tiny things have changed:

UISegmentedControl.appearance().setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.red as Any], for: .selected)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.