Ios 当UISegmentedControl值更改时,如何仅更改文本颜色?
我正在我的应用程序中使用Ios 当UISegmentedControl值更改时,如何仅更改文本颜色?,ios,objective-c,iphone,uisegmentedcontrol,Ios,Objective C,Iphone,Uisegmentedcontrol,我正在我的应用程序中使用UISegmentedControl,我可以在更改状态时切换文本颜色。但默认情况下,选定段包含不同的着色颜色,如下图所示 当处于选定状态时,我不希望有不同的着色颜色。我只想通过文本颜色区分所选片段,如下图所示 我知道这是一个愚蠢的问题,但其他类似的问题都没有给我一个正确的答案 你能指导我实现这一目标吗 期待中的感谢 查找下面的customsegment控件 #import <UIKit/UIKit.h> #import <QuartzCore/Qua
UISegmentedControl
,我可以在更改状态时切换文本颜色。但默认情况下,选定段包含不同的着色颜色,如下图所示
当处于选定状态时,我不希望有不同的着色颜色。我只想通过文本颜色区分所选片段,如下图所示
我知道这是一个愚蠢的问题,但其他类似的问题都没有给我一个正确的答案
你能指导我实现这一目标吗
期待中的感谢 查找下面的customsegment控件
#import <UIKit/UIKit.h>
#import <QuartzCore/QuartzCore.h>
@interface CustomSegmentControl : UISegmentedControl
@end
#import "CustomSegmentControl.h"
@interface CustomSegmentControl ()
- (UIImage *)imageWithColor:(UIColor *)color;
@end
@implementation CustomSegmentControl
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
return self;
}
- (void)drawRect:(CGRect)rect {
[super drawRect:rect];
[self setBackgroundImage:[[UIImage imageNamed:@"img.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0)] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
[self setBackgroundImage:[[UIImage imageNamed:@"img.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0)] forState:UIControlStateSelected barMetrics:UIBarMetricsDefault];
}
@end
#导入
#进口
@接口CustomSegmentControl:UISegmentedControl
@结束
#导入“CustomSegmentControl.h”
@接口自定义控件()
-(UIImage*)带有颜色的图像:(UIColor*)颜色;
@结束
@自动控制的实现
-(id)initWithFrame:(CGRect)帧
{
self=[super initWithFrame:frame];
如果(自我){
//初始化代码
}
回归自我;
}
-(void)drawRect:(CGRect)rect{
[超级drawRect:rect];
[self-setBackgroundImage:[[UIImage ImageName:@“img.png”]大小可调整的图像带电容插图:UIEdgeInsetsMake(0,0,0,0)]用于状态:UIControlStateNormal barMetrics:UIBarMetricsDefault];
[self-setBackgroundImage:[[UIImage ImageName:@“img.png”]大小可调整的图像带电容插图:UIEdgeInsetsMake(0,0,0,0)]用于状态:UIControlStateSelected barMetrics:UIBarMetricsDefault];
}
@结束
这将解决您的问题这就足够了
NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys:
[UIFont boldSystemFontOfSize:17], UITextAttributeFont,
[UIColor blackColor], UITextAttributeTextColor,
nil];
[_segment setTitleTextAttributes:attributes forState:UIControlStateNormal];
NSDictionary *highlightedAttributes = [NSDictionary dictionaryWithObject:[UIColor whiteColor] forKey:UITextAttributeTextColor];
[_segment setTitleTextAttributes:highlightedAttributes forState:UIControlStateHighlighted];
如果要在线段更改时更改颜色
- (IBAction)horseSegmentChanged:(UISegmentedControl*)sender {
if (sender.selectedSegmentIndex == 0) {
NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys:
[UIFont boldSystemFontOfSize:17], UITextAttributeFont,
[UIColor blackColor], UITextAttributeTextColor,
nil];
[_segment setTitleTextAttributes:attributes forState:UIControlStateNormal];
NSDictionary *highlightedAttributes = [NSDictionary dictionaryWithObject:[UIColor whiteColor] forKey:UITextAttributeTextColor];
[_segment setTitleTextAttributes:highlightedAttributes forState:UIControlStateHighlighted];
} else if (sender.selectedSegmentIndex == 1) { // selected shared blogs
}
}
我强烈建议不要这样做 你意识到这只会让它更难使用,对吗?对于一个色盲(大约11分之一的男性是色盲)来说,你的版本几乎不可能使用。对于视力差的人来说,很难使用它。可用性的最佳测试(以及苹果工程师的建议)是将它转换成灰度图像,看看你是否还能使用它 例如 部分色调 变成 在所选索引上使用对比色文本将改善这一点 您的配色方案 变成
这很难决定选择哪一部分。我同意福格迈斯特的观点,这在可用性方面可能不是一个好主意。也就是说,UISegmentedControl允许您为特定状态设置背景图像。您可以为每个UISegmentedControl状态设置具有相同颜色的图像,这将为您提供所需的效果 要以编程方式从UIColor创建图像,请参见 以及始终为白色背景的分段控件的一些示例代码(将UIImage类别视为上述类别):
谢谢你的回复。我认为这对我不起作用。请再次阅读问题“当文本改变状态时,我可以切换文本颜色”。我的要求是改变所选片段的背景色(色调),如果你注意到第一幅图像,你可以看到两种不同的颜色(白色和灰色),我只是不想要这两种颜色。我只想要一种颜色(看第二张图片),即使它被选中。我在更改textcolor时没有任何问题
[self.segmentedControl setBackgroundImage:[UIImage imageWithColor:[UIColor whiteColor]] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
[self.segmentedControl setBackgroundImage:[UIImage imageWithColor:[UIColor whiteColor]] forState:UIControlStateSelected barMetrics:UIBarMetricsDefault];
[self.segmentedControl setBackgroundImage:[UIImage imageWithColor:[UIColor whiteColor]] forState:UIControlStateHighlighted barMetrics:UIBarMetricsDefault];