// MARK: - 懒加载
private lazy var placeholderLabel: UILabel =
{
let lb = UILabel()
lb.textColor = UIColor.lightGray
lb.text = "占位文字"
lb.font = self.font
return lb
}()
-
将创建的label加入到UITextView中 布局label的位置
addSubview(placeholderLabel)
placeholderLabel.snp.makeConstraints { (make) in
make.top.equalTo(5)
make.left.equalTo(5)
}
-
使用通知监听UITextView是否有输入数据,有数据隐藏label
NotificationCenter.default.addObserver(self, selector: #selector(textChange), name: NSNotification.Name.UITextViewTextDidChange, object: self)
func textChange(){
//hasText 可以监听UITextView是否有值输入返回Bool类型
placeholderLabel.isHidden = hasText
}
deinit {
NotificationCenter.default.removeObserver(self)
}
网友评论