2014-09-29 118 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];