使用纯白色文本创建半透明uibutton

时间:2016-10-12 18:01:22

标签: ios swift xcode uistoryboard

我正在尝试创建类似于以下内容的按钮: enter image description here

我试图用白色文字颜色给alpha,但我得到了以下内容:

enter image description here

修改

textfield.backgroundColor = UIColor(red: 0.7, green: 0.7, blue: 0.7, alpha: 0.7)
textfield.textColor = .white

上面的代码给出了第二张图片。但上面的图像看起来很难看。

如何像第一张图片一样创建uitextfield?

修改
完整的解决方案:

txtLogin.backgroundColor = UIColor(red: 1, green: 1, blue: 1, alpha: 0.1)
    txtLogin.textColor = .white
    txtLogin.attributedPlaceholder = NSAttributedString(string:"Username",
                                                      attributes:[NSForegroundColorAttributeName: UIColor(red: 245, green: 245, blue: 241, alpha: 0.7)])
    let paddingView = UIView(frame: CGRect(x: 0, y: 0, width: 10, height: 20))
    txtLogin.leftView = paddingView
    txtLogin.leftViewMode = UITextFieldViewMode.always

    let layer: CALayer! = txtLogin.layer
    layer.cornerRadius = 4.0
    layer.borderWidth = 0

此致

1 个答案:

答案 0 :(得分:2)

您需要做的是将按钮的alpha设置为您想要的点。因此,例如,这将具有半透明背景和白色文本颜色:

button.backgroundColor = UIColor(red: 1, green: 1, blue: 1, alpha: 0.1)
button.textColor = .white
button.attributedPlaceholder = NSAttributedString(string:"placeholder text",
                                                   attributes:[NSForegroundColorAttributeName: UIColor.white])