【问题标题】:UIColor not working with RGBA valuesUIColor 不适用于 RGBA 值
【发布时间】:2014-08-10 05:35:57
【问题描述】:

我正在尝试使用以下代码(RGBA 值)更改 UITextField 中的文本颜色,但是它只是显示为白色或清晰,我不太确定,因为背景本身是白色的。

passwordTextField.textColor = UIColor(red: CGFloat(202.0), green: CGFloat(228.0), blue: CGFloat(230.0), alpha: CGFloat(100.0))

passwordTextField.returnKeyType = UIReturnKeyType.Done
passwordTextField.placeholder = "Password"
passwordTextField.backgroundColor = UIColor.clearColor()
passwordTextField.borderStyle = UITextBorderStyle.RoundedRect
passwordTextField.font = UIFont(name: "Avenir Next", size: 14)
passwordTextField.textAlignment = NSTextAlignment.Center
passwordTextField.secureTextEntry = true

【问题讨论】:

  • 202.0/255.0, 228.0/255.0, 230.0/255.0, 等等...这些值应该在0.01.0之间...

标签: ios swift uicolor


【解决方案1】:

red、green、blue 和 alpha 应该在 0.0 和 1.0 之间。

【讨论】:

    【解决方案2】:

    UIColor 的 RGB 值介于 0 和 1 之间(参见 the documentation“指定为 0.0 到 1.0 的值”)

    您需要将数字除以 255:

    passwordTextField.textColor = UIColor(red: CGFloat(202.0/255.0), green: CGFloat(228.0/255.0), blue: CGFloat(230.0/255.0), alpha: CGFloat(1.0))
    

    另外,你不需要创建 CGFloats:

    passwordTextField.textColor = UIColor(red:202.0/255.0, green:228.0/255.0, blue:230.0/255.0, alpha:1.0)
    

    【讨论】:

    • 两年后,这仍然是唯一的方法!令人惊讶的是,在其他任何地方都很少提及!
    【解决方案3】:

    试试这个:

    passwordTextField.textColor = UIColor(red: 0.792, green: 0.894, blue: 0.901, alpha: 1.0
    

    始终放置替换值。 202/255 = 0.792

    【讨论】:

      【解决方案4】:

      正如其他人所提到的,UIColor 组件在 0.0 ~ 1.0 范围内进行归一化(我认为广色域是例外,但尚未对此进行研究)。

      UIColor 类的便捷扩展 将允许您使用 0~255 范围内的值(如从各种检查器和图像编辑工具获得的值):

      import UIKit
      
      extension UIColor {
      
          convenience init(
              redByte   red:UInt8,
              greenByte green:UInt8,
              blueByte  blue:UInt8,
              alphaByte alpha:UInt8
              ) {
              self.init(
                  red:   CGFloat(red  )/255.0,
                  green: CGFloat(green)/255.0,
                  blue:  CGFloat(blue )/255.0,
                  alpha: CGFloat(alpha)/255.0
              )
          }
      }
      

      【讨论】:

        【解决方案5】:

        使用方便的 init(像专业人士一样的代码)

        第一步

        extension UIColor {
            convenience init(r: CGFloat, g: CGFloat, b: CGFloat) {
                self.init(red: r/255, green: g/255, blue: b/255, alpha: 1)
            }
        }
        

        用法

        //let color = UIColor(red: 202/255, green: 228/255, blue: 230/255, alpha: 1) ☠️
        let color = UIColor(r: 202, g: 228, b: 230) // ?
        

        【讨论】:

        • 只有一步? :P
        【解决方案6】:

        UIColor 方便的方法,来自 Integers,或者来自 Hex。

        extension UIColor {
        
        
            convenience init(red: Int, green: Int, blue: Int, alpha: CGFloat) {
                assert(red >= 0 && red <= 255, "Invalid red component")
                assert(green >= 0 && green <= 255, "Invalid green component")
                assert(blue >= 0 && blue <= 255, "Invalid blue component")
        
                self.init(red: CGFloat(red) / 255.0, green: CGFloat(green) / 255.0, blue: CGFloat(blue) / 255.0, alpha: alpha)
            }
        
        
        
            convenience init(rgb: Int, alpha: CGFloat = 1) {
                self.init(
                    red: (rgb >> 16) & 0xFF,
                    green: (rgb >> 8) & 0xFF,
                    blue: rgb & 0xFF,
                    alpha: alpha
                )
            }
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-06-15
          • 2011-08-04
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多