적당한 고통은 희열이다

- 댄 브라운 '다빈치 코드' 중에서

Swift iOS 앱 개발/Swift

[Swift iOS] UITextView set placeholder

hongssup_ 2021. 12. 9. 10:22
반응형

textField에는 있는 placeHolder가 textView에는 없다! 

수동으로 추가해주는 법. 

 

class UIViewController: UIViewController, UITextViewDelegate {
    @IBOutlet weak var myTextView: UITextView! 
    
    override func viewDidLoad() {
        myTextView.delegate = self
        myTextView.text = "내용을 입력하세요."
        myTextView.textColor = UIColor.systemGray3
    }
    
    //MARK: - TextView Delegate
    func textViewDidBeginEditing(_ textView: UITextView) {
        if textView.text == "내용을 입력하세요." {
            textView.text = ""
            textView.textColor = UIColor.black
        }
    }
    
    func textViewDidEndEditing(_ textView: UITextView) {
        if textView.text.isEmpty {
            textView.text = "내용을 입력하세요."
            textView.textColor = UIColor.systemGray3
        }
    }
}

 

 

 

참고 : tistory - Doony Garage

 


이딴 야매 방법 말고 그냥 placeholder label 하나 만들어줘서 isHidden true / false 바꿔주는게 더 나음,, ㅋㅋㅋ

728x90
반응형