Reputation: 1088
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..
Upvotes: 14
Views: 26707
Reputation: 2237
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];
Upvotes: 1
Reputation: 2403
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)
Upvotes: 13
Reputation: 1941
In addition to @Babl Prabhakar's answer
Swift 5:
UISegmentedControl.appearance().setTitleTextAttributes([NSAttributedString.Key.foregroundColor: UIColor.white], for: .selected)
Upvotes: 4
Reputation: 6927
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
Upvotes: 41
Reputation: 7922
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)
Upvotes: 0
Reputation: 4359
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];
Upvotes: 21
Reputation: 318774
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.
Upvotes: 2