gpt4 book ai didi

ios - 以编程方式添加不可见的标签

转载 作者:行者123 更新时间:2023-11-29 00:17:16 24 4
gpt4 key购买 nike

我想在文本字段顶部添加一个标签。因此,为此我正在使用扩展程序并以编程方式创建标签,但我看不到它。

扩展

extension UITextField{

func addPlaceHolder(mainView: UIView, placeHolderText: String) -> UILabel {
let label = UILabel()
label.text = placeHolderText
label.font = label.font.withSize(14)
label.textColor = Colors.hintColor
label.translatesAutoresizingMaskIntoConstraints=false
mainView.addSubview(label)
var leadingAnchorConst: NSLayoutConstraint?
var centerConst: NSLayoutConstraint?

if #available(iOS 9.0, *) {
leadingAnchorConst = label.leadingAnchor.constraint(equalTo: self.leadingAnchor, constant:10)
} else {
// Fallback on earlier versions
}
if #available(iOS 9.0, *) {
centerConst = label.centerYAnchor.constraint(equalTo: self.centerYAnchor)
} else {
// Fallback on earlier versions
}
NSLayoutConstraint.activate([leadingAnchorConst!, centerConst!])
return label
}

}

View Controller

class Test:UIViewController{

viewDidLoad(){
emailPH = tfEmail.addPlaceHolder(mainView: self.view, placeHolderText: "Email")
passwordPH = tfPassword.addPlaceHolder(mainView: self.view, placeHolderText: "Password")
print(passwordPH?.text ?? "")
}


}

最佳答案

请检查一下此代码

 let codedLabel:UILabel = UILabel()
codedLabel.frame = CGRect(x: 100, y: 100, width: 200, height: 200)
codedLabel.textAlignment = .center
codedLabel.text = alertText
codedLabel.numberOfLines=1
codedLabel.textColor=UIColor.red
codedLabel.font=UIFont.systemFont(ofSize: 22)
codedLabel.backgroundColor=UIColor.lightGray

self.contentView.addSubview(codedLabel)
codedLabel.translatesAutoresizingMaskIntoConstraints = false
codedLabel.heightAnchor.constraint(equalToConstant: 200).isActive = true
codedLabel.widthAnchor.constraint(equalToConstant: 200).isActive = true
codedLabel.centerXAnchor.constraint(equalTo:
codedLabel.superview!.centerXAnchor).isActive = true
codedLabel.centerYAnchor.constraint(equalTo:
codedLabel.superview!.centerYAnchor).isActive = true

关于ios - 以编程方式添加不可见的标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45030901/

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