【问题标题】:Adjust alpha of UIColor调整 UIColor 的 alpha
【发布时间】:2015-09-04 23:15:45
【问题描述】:

我使用 rgb 将UIColor 设置为UILabel 的背景。我正在尝试仅调整alpha。如何修改现有 rgb UIColor 的 alpha?

编辑

基本上我有UILabels 有一组UIColor(使用rgb),我不知道UILabels 是什么颜色。在某个时候,我将不得不更改labels alpha`。如何更改标签颜色 alpha?

【问题讨论】:

  • 属于预定义的UIColor?对于标签,您可以使用 .alpha 属性。
  • 感谢您的回复!。我有一个预定义的 UIColor。我不想设置标签 alpha,而是设置颜色 alpha

标签: ios objective-c alpha uicolor


【解决方案1】:

colorWithAlphaComponent: 成功了。

所以我要做的是:

self.mylabel.backgroundColor = [self.myLabel.backgroundColor colorWithAlphaComponent:0.3];

【讨论】:

  • 这种做法和说这个有什么区别吗? UIColor(red:0.07, green:0.44, blue:0.54, alpha:0.4)
  • @Changerrs :在某些情况下,我们需要将 alpha 应用于现有颜色。即不能修改现有颜色。因此,对于这种情况,我们已经有了一个颜色值,但随后需要对其应用 alpha,因此 colorWithAlphaComponent 在这种情况下会派上用场。
【解决方案2】:

Swift 3+

yourUIView.backgroundColor = UIColor.white.withAlphaComponent(0.75)

斯威夫特 2

yourUIView.backgroundColor = UIColor.whiteColor().colorWithAlphaComponent(0.75)

【讨论】:

    【解决方案3】:

    如果您定义了自定义颜色,您也可以这样做:

    view.backgroundColor = [[MyClass someColor] colorWithAlphaComponent:0.25];
    

    【讨论】:

      【解决方案4】:

      为什么不使用 label.alpha = 0.5 ?调整标签的 alpha?

      更新:如果你想从旧颜色调整 alpha,这里有一个例子:

      UIColor uicolor = [UIColor greenColor];
      CGColorRef color = [uicolor CGColor];
      
      int numComponents = CGColorGetNumberOfComponents(color);
      
      UIColor newColor;
      if (numComponents == 4)
      {
          const CGFloat *components = CGColorGetComponents(color);
          CGFloat red = components[0];
          CGFloat green = components[1];
          CGFloat blue = components[2];
          newColor = [UIColor colorWithRed: red green: green blue: blue alpha: YOURNEWALPHA];
      
      }
      

      【讨论】:

      • 我不想设置标签 alpha,我想设置标签颜色 alpha
      • 区别在哪里?
      • 如果我设置了整个标签的alpha,那么文本也会变浅,因为我设置了标签的背景色
      • @Eric 你的意思是从现有的 UIColor 更改颜色的 alpha 吗?
      【解决方案5】:

      版本 Swift (5.4)

      UIColor.blue.withAlphaComponent(0.5)
      

      【讨论】:

        【解决方案6】:

        尽可能短的代码(Swift 5):

        view.backgroundColor = .black.withAlphaComponent(0.75)
        

        【讨论】:

          【解决方案7】:

          请在下面的代码中设置 alpha -

          [UIColor colorWithRed:200.0/255.0 green:191.0/255.0 blue:231.0 /255.0 alpha:1.0]
          

          【讨论】:

          • 我有那个代码,但我只想修改 alpha
          • 你想设置 UIlabel 的背景颜色并想改变 UILabel 的 alpha 对吗?请使用上面的代码
          • [UIColor colorWithRed:200.0/255 green:191.0/255 blue:231.0/255 alpha:1.0] 这样修改
          • 是的,对的,抱歉搞错了
          猜你喜欢
          • 2013-12-23
          • 1970-01-01
          • 2013-03-30
          • 2018-03-24
          • 2022-12-31
          • 2018-12-23
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多