【问题标题】:Change text color of UISegmentedControl更改 UISegmentedControl 的文本颜色
【发布时间】:2012-10-17 07:57:08
【问题描述】:

我是目标c的新手,需要在UIsegmentControl中更改选定段的文本颜色。 使用以下代码。

 [[UIsegmentControl.subviews objectAtIndex:segment.selectedSegmentIndex] setTintColor:[UIColor redColor]];

它改变了段颜色。请帮帮我..

【问题讨论】:

标签: objective-c xcode


【解决方案1】:

没有标准的 API 来设置 UISegmentedControl 中单个段的文本属性。您可以采用不推荐的方法来挖掘分段控件的视图层次结构,找到所需的 UILabel(如果有)并设置该标签的属性。更好的方法是找到(或编写)模拟 UISegmentedControl 并允许以您需要的方式自定义单个段的自定义控件。

编辑:

实际上,我是从错误的角度看待这个问题的。我的回答是基于尝试为特定段索引设置属性。但实际上这可以通过设置UIControlStateSelected状态的文本属性来实现。很抱歉造成混乱。

【讨论】:

  • 您可以使用 UIAppearance 方法为特定的控件状态设置文本属性。所以这个答案是错误的。
  • @jrturton 当然。我的错。我是从段索引的角度考虑这个问题的。但在这种情况下,它可以为突出显示的状态完成。我会更新我的答案。谢谢
【解决方案2】:

无法在UISegmentedControl 中设置所选段标题的自定义颜色。 forState: 中的UIControlState 用于设置段文本为正常和选中状态的属性。

来自您的代码:

[[UIsegmentControl.subviews objectAtIndex:segment.selectedSegmentIndex] setTintColor:[UIColor redColor]];

试试这个代码:

[segmnt_cntrl setTitleTextAttributes:@{NSFontAttributeName:[UIFont fontWithName:@"Arial" size:16.0],
                                                          NSForegroundColorAttributeName:[UIColor redColor],
                                                          NSShadowAttributeName:shadow}
                                                         forState:UIControlStateNormal];

将 segmnt_cntrl 替换为您的 Segment Cotrol 对象。试试这个,它可能会帮助你实现你的总体目标。

谢谢

【讨论】:

  • 这个答案是正确的,除了你的第一段。 forState: 中的UIControlState 可用于设置普通和选定段文本的属性。
  • @PuneethSB 太好了。如果它适合您,请接受答案。 :)
  • 属性的范围呢?我想在一个标题中有两种不同的颜色。标准 UISegmentedControl 可以吗?
  • 嗨。什么是 NSShadowAttributeName:阴影?我可以找到影子名称属性
【解决方案3】:

如果您需要在 iOS 7 中更改高亮段的文本颜色,这里有一个解决方案(花了我一段时间才找到,但是 thanks to this post):

[[UISegmentedControl appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor colorBlack]} forState:UIControlStateSelected];

【讨论】:

  • 如果我想将突出显示的文本颜色以及普通段的文本颜色更改为黑色,请告诉我该怎么做。现在它是蓝色的
  • @NehaDangui,将UIControlStateSelected 替换为UIControlStateNormal(或UIControlStateHighlighted,如果需要)。
【解决方案4】:

引用@i--回答

更新了 Swift 5

UISegmentedControl.appearance().setTitleTextAttributes([NSAttributedString.Key.foregroundColor: UIColor.red], for: .selected)

更新了 Swift 4.1

UISegmentedControl.appearance().setTitleTextAttributes([NSAttributedStringKey.foregroundColor: UIColor.red], for: .selected)

斯威夫特 3.1

UISegmentedControl.appearance().setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.red as Any], for: .selected)

对于早期的 Swift 版本:

UISegmentedControl.appearance().setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.red], for: .selected)

【讨论】:

  • 斯威夫特 5:UISegmentedControl.appearance().setTitleTextAttributes([NSAttributedString.Key.foregroundColor: UIColor.white], for: .selected)
【解决方案5】:

更新了@Babul Prabhakar 对 Swift 3 的回答,因为大约有六个小事情发生了变化:

UISegmentedControl.appearance().setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.red as Any], for: .selected)

【讨论】:

    【解决方案6】:

    除了@Babl Prabhakar 的回答

    斯威夫特 5: UISegmentedControl.appearance().setTitleTextAttributes([NSAttributedString.Key.foregroundColor: UIColor.white], for: .selected)

    【讨论】:

      【解决方案7】:

      目标 C

      设置字体样式、大小和颜色

      NSDictionary *attributes = [NSDictionary dictionaryWithObject: [UIFont fontWithName: @"Arial" size:16] forKey:NSFontAttributeName];
      [self->mySegmentControl setTitleTextAttributes: attributes forState: normal];
      [self->mySegmentControl setTitleTextAttributes: @{NSForegroundColorAttributeName: UIColor.blueColor} forState: UIControlStateSelected];
      

      【讨论】:

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