LoginSignup
5
5

More than 5 years have passed since last update.

Swift3 - UITextViewにPlaceholderをLabelを使わずに実装する方法

Last updated at Posted at 2017-05-05

環境

  • Swift3
  • Xcode8
  • Mac Os Sierra

コピペで動くはず

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
        }
    }
}

実行

on.gif

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を使わずに実装する方法

5
5
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
5