UITextView는 UITextField와 달리 placeholder가 없다. 그렇기에 placeholder를 적용시키고 싶다면 직접 만들어야하는 번거로움이 있다.
// ViewDidLoad()에 layout 지정과 addSubView는 별도!
// textview 선언
private lazy var textView: UITextView = {
let textView = UITextView()
textView.font = .systemFont(ofSize: 14, weight: .medium)
textView.text = "플레이스홀더~~"
textView.backgroundColor = .white
textView.textColor = .placeholderText
textView.delegate = self
return textView
}()
// extension(부분)
extension ViewController: UITextViewDelegate {
func textViewDidBeginEditing(_ textView: UITextView) {
guard textView.textColor == .placeholderText else { return }
textView.textColor = .label
textView.text = nil
}
func textViewDidEndEditing(_ textView: UITextView) {
if textView.text.isEmpty {
textView.text = "플레이스홀더~~"
textView.textColor = .placeholderText
}
}
}