简体   繁体   English

如何在UISegmentControl中更改文本颜色?

[英]How to change text color in UISegmentControl?

谁能建议我如何更改UISegment控件的文本颜色?

The Apple Documentation does not specify a method for doing this programatically. Apple文档未指定以编程方式执行此操作的方法。 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. 然后打开photoshop或gimp并以所需的颜色添加所需的文本。 Then tell the slider to you your images like so (Assumes 2 Segments): 然后像这样告诉滑块您的图像(假设2个细分):

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: 来自UICatalog的现代方法是:

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. setTitleTextAttributes:forState:首次出现在iOS 5中,因此这些天没有理由不使用它。

Worth mentioning: You'll probably want to set UITextAttributeTextShadowColor , too. 值得一提:您可能还需要设置UITextAttributeTextShadowColor

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM