UIColor不使用RGBA值

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


不负相思意
浏览 629回答 3
3回答

心有法竹

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))另外,你不需要创建CGFloats:passwordTextField.textColor = UIColor(red:202.0/255.0, green:228.0/255.0, blue:230.0/255.0, alpha:1.0)

慕仙森

使用方便init(代码像专业人士)步骤1extension 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) // ?

侃侃尔雅

试试这个:passwordTextField.textColor = UIColor(red: 0.792, green: 0.894, blue: 0.901, alpha: 1.0始终放置替代值。202/255 = 0.792
打开App,查看更多内容
随时随地看视频慕课网APP