iAppDeveloper iAppDeveloper - 4 months ago 43
Objective-C Question

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

Answer

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