ios - UIColor 不适用于 RGBA 值

标签 ios swift uicolor

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

最佳答案

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)

关于ios - UIColor 不适用于 RGBA 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38813125/

相关文章:

ios - 如何改变图像的一部分颜色而不是整个图像

更新的twitter api更新到Twitter1.11.3后,使用fabric登录iOS导致错误

ios - 为什么我的数组加载但我的 tableView 会崩溃,索引超出空数组的范围?

ios - 删除对象后重新加载 CollectionViewController?

ios - 具有可变参数的通用类型别名

xcode - Interface Builder 的颜色选择器中的颜色错误

ios - 如何将数据从 TableView 单元格传输到 View Controller

swift - Xcode Swift 使用 Toggles 将 Label 增加 1 并保存 Toggles 的值

macos - 在 Swift 中解码引用的可打印消息

ios - 在 Adob​​e 中打开时 PDF 的颜色与在预览中不同