【问题标题】:How to change only text color when UISegmentedControl value changed?UISegmentedControl 值更改时如何仅更改文本颜色?
【发布时间】:2014-09-29 11:23:26
【问题描述】:

我在我的应用程序中使用UISegmentedControl,当它改变状态时我可以切换文本颜色。但默认情况下,所选片段包含不同的色调,如下图所示

我不想在选择状态时有不同的色调颜色。我只想通过如下图的文本颜色来区分选定的段。

我知道这是一个愚蠢的问题,但其他类似的问题都没有为我提供正确的答案。

谁能指导我实现这一目标?

感谢期待!

【问题讨论】:

    标签: ios objective-c iphone uisegmentedcontrol


    【解决方案1】:

    我强烈建议不要这样做。

    您意识到这只会使其更难使用,对吧?对于色盲者(大约十分之一的男性是色盲),您的版本几乎无法使用。对于视力不佳的人来说,它很难使用。可用性的最佳测试(以及来自 Apple 工程师的建议)是将其转换为灰度图像,看看您是否仍然可以使用它。

    例如...

    部分色调

    变成……

    在所选索引上使用对比色文本会改善这一点。

    你的配色方案

    变成……

    这很难确定选择了哪个部分。

    【讨论】:

      【解决方案2】:

      这就够了。

       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
      
      
      
              }
      
      
           }
      

      【讨论】:

      • 感谢您的回复。我不认为这对我有用。请再次阅读问题“我可以在更改状态时切换文本颜色”。我的要求是更改所选片段的背景颜色(色调颜色),如果您注意到第一张图像您可以看到两种不同的颜色(白色和灰色),我只是不想要这两种颜色。我只想要一种颜色(看第二张图片),即使它被选中。
      • 我在更改文本颜色方面没有任何问题
      【解决方案3】:

      找到下面的海关监管

      #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
      

      这将解决您的问题

      【讨论】:

        【解决方案4】:

        我同意 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];
        

        【讨论】:

          猜你喜欢
          • 2012-10-17
          • 2014-02-05
          • 2015-06-28
          • 2022-01-21
          • 1970-01-01
          • 2012-02-20
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多