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..

iAppDeveloper
  • 1,088
  • 1
  • 8
  • 16
  • Updated answer for swift4 with extension, https://stackoverflow.com/questions/9029760/how-to-change-font-color-of-uisegmentedcontrol/50795377#50795377 – Soumen Jun 11 '18 at 10:16

7 Answers7

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

CaptJak
  • 3,592
  • 1
  • 29
  • 50
Harjot Singh
  • 6,767
  • 2
  • 38
  • 34
  • 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 '12 at 07: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 '13 at 11:15
  • hi. WHat is NSShadowAttributeName: shadow? I can find shadow name attribute – kemdo Oct 05 '17 at 06: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];
Community
  • 1
  • 1
i--
  • 4,349
  • 2
  • 27
  • 35
  • 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 – Neha Dangui Mar 26 '14 at 11:59
  • @NehaDangui, it would be a matter of replacing `UIControlStateSelected` with `UIControlStateNormal` (or `UIControlStateHighlighted` if you need to). – i-- Mar 26 '14 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)
Babul Prabhakar
  • 2,373
  • 1
  • 18
  • 25
  • Swift 5: `UISegmentedControl.appearance().setTitleTextAttributes([NSAttributedString.Key.foregroundColor: UIColor.white], for: .selected)` – Sravan Apr 07 '19 at 19:46
4

In addition to @Babl Prabhakar's answer

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

Sravan
  • 1,891
  • 4
  • 23
  • 28
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.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
  • You can set text attributes using the UIAppearance methods, for a specific control state. So this answer is wrong. – jrturton Oct 27 '12 at 07: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 '12 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];
Deepak Tagadiya
  • 2,187
  • 15
  • 28
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)
Echelon
  • 7,306
  • 1
  • 36
  • 34