macOS, iOS

[iOS] textview에 placeholder 추가하기

SweetDev 2020. 7. 3. 10:21

 

viewDidLoad에서 delegate설정

 

    /// textview 설정

commentTextView.delegate = self
commentTextView.text =  "댓글 내용 입력"
commentTextView.textColor = UIColor.darkGray

    

 

extension 써주기

extension CreatePostingViewController: UITextViewDelegate {
  func textViewDidBeginEditing(_ textView: UITextView) {
    if textView.textColor == UIColor.lightGray {
      textView.text = nil
      textView.textColor = UIColor.black
    }
  }

  func textViewDidEndEditing(_ textView: UITextView) {
    if textView.text.isEmpty {
      textView.text = "내용을 입력해주세요."
      textView.textColor = UIColor.lightGray
    }
  }
}

 

[출처]

stackoverflow.com/questions/27652227/how-can-i-add-placeholder-text-inside-of-a-uitextview-in-swift