【问题标题】:How to check the colour of the text on a UIButton?如何检查 UIButton 上文本的颜色?
【发布时间】:2013-09-05 21:47:39
【问题描述】:

听起来很简单,对吧?好吧,我已经用谷歌搜索了十几个搜索字符串,但仍然找不到答案。

要设置颜色,我们实现以下代码:

[_myUIButton setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];

但是测试一下按钮文本的颜色是什么?这当然是可能的,对吧?

代码不应该是这样的吗:

if([_myUIButton.textColor isEqual:[UIColor blueColor]])
{
    // then true
}

此代码不适用于按钮标题文本颜色,但必须有人知道如何做到这一点。

请帮忙。

【问题讨论】:

  • 为什么需要检查颜色?你不是根据状态设置颜色吗?你为什么不检查状态而不是颜色?您可能还没有找到任何东西,因为不需要检查文本的颜色...

标签: ios objective-c uibutton textcolor


【解决方案1】:
if([button titleColorForState:UIControlStateNormal] isEqual:[UIColor blackColor]]){

  }

【讨论】:

    【解决方案2】:

    如有疑问,请查看Apple docs

    - (UIColor *)titleColorForState:(UIControlState)state;
    

    【讨论】:

      【解决方案3】:

      您可以使用 UIButton 的 currentTitleColor 属性:

      if ([button.currentTitleColor isEqual:[UIColor blueColor]]) {
          // Do something
      }
      

      【讨论】:

        【解决方案4】:

        NSLog(@"颜色为 %@",[btn currentTitleColor]);这将为您提供黑色的答案 0 0 0 1 这意味着红色为 0 绿色为 0 而蓝色为 0 且 alpha 为 1,这样您就可以找到任何颜色的 RGB,或者您可以使用 if 条件来匹配特定颜色

        【讨论】:

          【解决方案5】:

          你也可以使用

          if([myButton.titleLabel.textColor  isEqual:[UIColor blueColor]]){
              // your code
          }
          

          【讨论】:

            猜你喜欢
            • 2021-09-13
            • 2012-07-14
            • 2011-10-05
            • 2019-07-27
            • 1970-01-01
            • 2015-09-14
            • 2022-07-26
            • 2022-08-04
            • 2020-11-05
            相关资源
            最近更新 更多