Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change text color of UISegmentedControl

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

like image 850
iAppDeveloper Avatar asked Oct 27 '12 04:10

iAppDeveloper


People also ask

How do you change the color of your text?

Go to Format > Font > Font. + D to open the Font dialog box. Select the arrow next to Font color, and then choose a color. Select Default and then select Yes to apply the change to all new documents based on the template.

How do I change text color in segmented control?

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

How do I change the color of a segment in Swift?

To change the overall color of the segmented control use its backgroundColor . To change the color of the selected segment use selectedSegmentTintColor . To change the color/font of the unselected segment titles, use setTitleTextAttributes with a state of . normal / UIControlStateNormal .


2 Answers

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

like image 114
Harjot Singh Avatar answered Oct 13 '22 23:10

Harjot Singh


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];
like image 24
i-- Avatar answered Oct 13 '22 21:10

i--