gpt4 book ai didi

ios - 如何以编程方式制作可重用的 UILabel 和 UITextField

转载 作者:搜寻专家 更新时间:2023-11-01 06:55:15 25 4
gpt4 key购买 nike

我想制作一个可重用的 UILabel 和 UITextField,这样它就不会重复,我该怎么做?这是我的代码

let nameLabel: UILabel = {
let label = UILabel()
label.text = "Name"
label.textColor = .white
return label
}()

let nameTextField: UITextField = {
let tv = UITextField()
tv.placeholder = "Name"
tv.textColor = #colorLiteral(red: 0.6078431373, green: 0.6078431373, blue: 0.6078431373, alpha: 1)
return tv
}()

let emailLabel: UILabel = {
let label = UILabel()
label.text = "Email"
label.textColor = .white
return label
}()

let emailTextField: UITextField = {
let tv = UITextField()
tv.placeholder = "Email"
tv.textColor = #colorLiteral(red: 0.6078431373, green: 0.6078431373, blue: 0.6078431373, alpha: 1)
return tv
}()

最佳答案

使用计算属性,每次访问都会返回一个新的实例

var emailLabel: UILabel  {
let label = UILabel()
label.text = "Default"
label.textColor = .green
view.addSubview(label)
return label
}

let em1 = emailLabel 
em1.text = "dshjsdhdshdhdshjdsh"
em1.frame = CGRect(x: 0, y: 0, width: 100, height: 100)


let em2 = emailLabel
em2.text = "dshjsdhdshdhdshjdsh"
em2.frame = CGRect(x: 0, y: 100, width: 100, height: 100)

关于ios - 如何以编程方式制作可重用的 UILabel 和 UITextField,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53911880/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com