当文本字段的背景设置为透明时,是否可以使文本字段的占位符文本不透明,例如。如果我选择一些背景颜色,并将alpha设置为某个值,如下所示:
占位符文本也变得透明。但我希望它不透明。那么,这是否可以实现,最好使用故事板?或者至少通过代码。
如果不清楚我想要实现的目标,请告诉我,我将发布一个带有示例的图片。
答案 0 :(得分:1)
您可以设置颜色的透明度而不是UITextField的alpha。打开颜色下拉菜单并选择“其他”。颜色选择器将打开。在颜色选择器的底部,您可以更改不透明度。
答案 1 :(得分:1)
在 Swift 中,您可以获取占位符元素:
let placeHolder = textField.value(forKey: "placeholderLabel") as! UILabel
placeHolder.textColor = .blue
placeHolder.isOpaque = true
并进行所有自定义设置..
答案 2 :(得分:0)
您可以设置 UITextView.background = .clear
我提供了更多代码,以便以编程方式设置它更容易,这是一个随时可用的示例。
private lazy var textField: UITextView = {
let textField = UITextView()
textField.translatesAutoresizingMaskIntoConstraints = false
textField.font = UIFont.systemFont(ofSize: 14)
textField.isEditable = false
textField.textAlignment = .center
textField.isScrollEnabled = false
textField.backgroundColor = .clear // this is the line that answer you question
textField.text = """
People with these symptoms may have COVID-19:
Fever of chills, cough, shortness of breath or
difficulty breathing, fatigue, muscle or body aches,
headache, new loss of taste and smell, sore throat,
congestion or runny nose, nausea or vomiting,
diarrhea
"""
return textField
}()
只需记住设置约束并将子视图添加到您的超级视图中 .addSubview(TextField)