Zhen
Zhen

Reputation: 12431

How to change text color in UISegmentControl?

Can anyone advise me on how I can change the text color of a UISegment Control?

Upvotes: 0

Views: 348

Answers (2)

Steven Fisher
Steven Fisher

Reputation: 44856

I realize this is an old question, but it was the first relevant hit on this.

The modern way, from UICatalog, is:

NSDictionary *textAttributes = @{ UITextAttributeTextColor:[UIColor blueColor],
                                  UITextAttributeFont:[UIFont systemFontOfSize:13.0] };
[segmentedControl setTitleTextAttributes:textAttributes
                                forState:UIControlStateNormal];

textAttributes = @{ UITextAttributeTextColor:[UIColor redColor],
                    UITextAttributeFont:[UIFont systemFontOfSize:13.0] };
[segmentedControl setTitleTextAttributes:textAttributes
                                forState:UIControlStateHighlighted];

setTitleTextAttributes:forState: first appeared in iOS 5, so there's no reason not to use it these days.

Worth mentioning: You'll probably want to set UITextAttributeTextShadowColor, too.

Upvotes: 0

rich
rich

Reputation: 2136

The Apple Documentation does not specify a method for doing this programatically. The easiest way to do this would be to take a screen shot of each of the segmented control segments without text on them. Then open up photoshop or gimp and add the desired text with whatever coloring you would like. Then tell the slider to you your images like so (Assumes 2 Segments):

UIImage *segmentOne = [[UIImage imageNamed:@"segmentOne.png"] stretchableImageWithLeftCapWidth:4 topCapHeight:0];
UIImage *segmentTwo = [[UIImage imageNamed:@"segmentTwo.png"] stretchableImageWithLeftCapWidth:4 topCapHeight:0];
[_segmentedControl setImage:segmentOne forSegmentAtIndex:0];
[_segmentedControl setImage:segmentTwo forSegmentAtIndex:1];

Upvotes: 3

Related Questions