UIColor不使用RGBA值 [英] UIColor not working with RGBA values

查看:150
本文介绍了UIColor不使用RGBA值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to change the text colour in a UITextField using the following code (RGBA value) however it just appears white, or clear, I'm not too sure as the background is white itself.

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之间(参见< a href =https://developer.apple.com/library/ios/documentation/uikit/reference/UIColor_Class/Reference/Reference.html#//apple_ref/occ/clm/UIColor/colorWithRed%3agreen%3ablue%3aalpha% 3a>文档指定为0.0到1.0之间的值)

RGB values for UIColor are between 0 and 1 (see the documentation "specified as a value from 0.0 to 1.0")

您需要将数字除以255:

You need to divide your numbers by 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 :

Another thing, you don't need to create CGFloats:

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

这篇关于UIColor不使用RGBA值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆