一尘不染

UIColor无法使用RGBA值

swift

我正在尝试使用以下代码(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

阅读 235

收藏
2020-07-07

共1个答案

一尘不染

UIColor的RGB值介于0和1之间(请参见文档
“指定为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))

另一件事,您不需要创建CGFloat:

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