2014-09-29 54 views
0

我在我的应用程序中使用UISegmentedControl,我可以在改变状态时切换文本颜色。但默认情况下选中段包含像下面的图像如何更改UISegmentedControl值更改时的文本颜色?

enter image description here

我不希望有不同的色调的颜色,当它处于选中状态的不同色调的颜色。我只是希望区分选定的部分只能通过像下面的图像的文字颜色。

enter image description here

我知道这是一个愚蠢的问题,但没有其他类似的问题,我提供一个合适的回答。

你能指导我实现这个吗?

感谢您的期待!

回答

1

我会强烈建议不这样做的颜色。

你意识到这只是让它难以正确使用吗?对于色盲人士(约11名男性中有1人是色盲),你的版本几乎不可能使用。对于视力不好的人来说,这很难使用。可用性的最佳测试(以及Apple工程师的建议)是将其转换为灰度图像,然后查看是否仍可使用它。

例如...

科色调

enter image description here

成为...

enter image description here

使用所选指标的对比色的文字将提高这个。

你的配色方案

enter image description here

成为...

enter image description here

这是非常难以确定选择哪个部分。

0

看到以下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 

这将解决您的问题

1

这是不够的。

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 



     } 


    } 
+0

感谢您的响应。我认为这不适合我。请再次阅读问题“我可以在改变状态时切换文本颜色”。我的要求是改变所选片段的背景颜色(色调颜色),如果你注意到第一个图像,你可以看到两种不同的颜色(白色和灰色),我只是不想要这两种颜色。即使选择了,我也只想要一种颜色(看第二张图片)。 – thavasidurai 2014-09-29 13:10:27

+0

我在更改textcolor时没有任何问题 – thavasidurai 2014-09-29 13:12:21

0

我同意Fogmeister在可用性方面这可能不是一个好主意。也就是说,UISegmentedControl允许您为特定状态设置背景图像。你可以为每个UISegmentedControl状态设置一个相同颜色的图像,这会给你你想要的效果。

要以编程方式从UIColor创建图像,请参阅here

以及用于与总是白色背景的分段控制(考虑一个的UIImage类别作为一个如上文引用)一些示例代码:

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