首页 > 解决方案 > 如何从 UITextField 中排除文本以使 alpha 透明?

问题描述

我创建了一个将 alpha 设置为 0.2 的 uitextfield,问题是我找不到如何将 alpha 不透明度设置为仅应用于 UITextField 的背景,而不是应用于 UItextField 内的文本。我的代码片段:

     self.emailInputField.text = "Enter your Email/Username"
    self.passwordEmailInputField.text = "Enter your Password"
    self.passwordEmailInputField.backgroundColor = .white
    self.emailInputField.backgroundColor = .white
    self.passwordEmailInputField.alpha = 0.1
    self.emailInputField.alpha = 0.1
    self.passwordEmailInputField.textColor = .gray
    self.passwordEmailInputField.textColor = .gray
    self.view.addSubview(self.emailInputField)
    self.view.addSubview(self.passwordEmailInputField)

标签: iosswift

解决方案


扩展上面的评论:alpha如果您的意图只是更改背景颜色,则不应将字段本身设置为 0.1。相反,backgroundColor使用 alpha 初始化属性:

self.passwordEmailInputField.backgroundColor = .white.withAlphaComponent(0.1)
self.emailInputField.backgroundColor = .white.withAlphaComponent(0.1)

推荐阅读