开发者

How to change text color in UISegmentControl?

开发者 https://www.devze.com 2023-03-25 09:46 出处:网络
Can anyone 开发者_JAVA技巧advise me on how I can change the text color of a UISegment Control?The Apple Documentation does not specify a method for doing this programatically. The easiest way to do th

Can anyone 开发者_JAVA技巧advise me on how I can change the text color of a UISegment Control?


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号