Swift3 - UITextView에서 Placeholder를 Label없이 구현하는 방법

환경


  • Swift3
  • Xcode8
  • Mac Os Sierra

  • copipe로 움직여야합니다.



    Outlet은 스토리 보드에서
    
    class AddTextViewController: UIViewController, UITextViewDelegate, UITextFieldDelegate {
    
    
        @IBOutlet weak var TitleText: UITextField!
        @IBOutlet weak var DetailText: UITextView!
    
    
        override func viewDidLoad() {
            super.viewDidLoad()
    
            // Do any additional setup after loading the view.
        }
    
        override func didReceiveMemoryWarning() {
            super.didReceiveMemoryWarning()
            // Dispose of any resources that can be recreated.
        }
    
        override func viewWillAppear(_ animated: Bool) {
            super.viewWillAppear(animated)
    
            TitleText.text = "タイトル"
            TitleText.textColor = UIColor.lightGray
            TitleText.delegate = self
    
            DetailText.text = "本文"
            DetailText.textColor = UIColor.lightGray
            DetailText.delegate = self
        }
    
        func textViewDidBeginEditing(_ textView: UITextView) {
    
            print("反応")
            if DetailText.textColor == UIColor.lightGray {
                DetailText.text = nil
                DetailText.textColor = UIColor.black
            }
        }
    
    
        func textViewDidEndEditing(_ textView: UITextView) {
            if DetailText.text.isEmpty {
                DetailText.text = "本文"
                DetailText.textColor = UIColor.lightGray
            }
        }
    
        func textFieldDidBeginEditing(_ textField: UITextField) {
            if TitleText.textColor == UIColor.lightGray {
                TitleText.text = nil
                TitleText.textColor = UIColor.black
            }
        }
    
        func textFieldDidEndEditing(_ textField: UITextField) {
            if (TitleText.text?.isEmpty)! {
                TitleText.text = "タイトル"
                TitleText.textColor = UIColor.lightGray
            }
        }
    }
    

    실행





    Step1


    UITextViewDelegate 대리자
    class MyViewController: UIViewController, UITextViewDelegate {
    

    step2


    viewWillAppear 에 아래와 같이 쓴다 bodyTextView 는 자신의 IBOutlet 입니다
    
        override func viewWillAppear(_ animated: Bool) {
            super.viewWillAppear(animated)
    
    
            bodyTextView.text = "Placeholder"
            bodyTextView.textColor = UIColor.lightGray
    
    
        }
    
    

    step3


    textViewDidBeginEditingtextViewDidEndEditing 구현
    
    
        func textViewDidBeginEditing(_ textView: UITextView) {
            if bodyTextView.textColor == UIColor.lightGray {
                bodyTextView.text = nil
                bodyTextView.textColor = UIColor.black
            }
        }
    
    
        func textViewDidEndEditing(_ textView: UITextView) {
            if bodyTextView.text.isEmpty {
                bodyTextView.text = "Placeholder"
                bodyTextView.textColor = UIColor.lightGray
            }
        }
    
    

    주의점



    덧붙여서 이것이라면 스토리 보드에 처음부터 붙어있는 textField의 Placeholder와는 움직임이 다르기 때문에 textField와는 병용이 불가능합니다. 그래서, textField 와 병용하는 경우는, textField 도 이번의 TextView 와 같이 구현하지 않으면 안됩니다.

    step4


    UITextField 대리자
    class MyViewController: UIViewController, UITextFieldDelegate, UITextViewDelegate {
    
    

    step5


    viewWillAppear 에 아래와 같이 쓴다 titleTextField 는 자신의 IBOutlet 입니다
    override func viewWillAppear(_ animated: Bool) {
            super.viewWillAppear(animated)
    
    
            bodyTextView.text = "Placeholder"
            bodyTextView.textColor = UIColor.lightGray
    
            titleTextField.text = "Placeholder"
            titleTextField.textColor = UIColor.lightGray
    
    
        }
    
    

    step6


    textFieldDidBeginEditingtextViewDidEndEditing 구현
    
          /*
    
         テキストフィールドもプレイスフォルダー実装
    
         */
    
        func textFieldDidBeginEditing(_ textField: UITextField) {
            if titleTextField.textColor == UIColor.lightGray {
                titleTextField.text = nil
                titleTextField.textColor = UIColor.black
            }
        }
    
    
        func textFieldDidEndEditing(_ textField: UITextField) {
            if (titleTextField.text?.isEmpty)! {
                titleTextField.text = "Placeholder"
                titleTextField.textColor = UIColor.lightGray
            }
        }
    
    

    참고



    Swift3 – UITextView에서 Placeholder를 Label없이 구현하는 방법

    좋은 웹페이지 즐겨찾기