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

# 環境

- Swift3
- Xcode8
- Mac Os Sierra


# コピペで動くはず

Outletはストーリーボードから

```swift

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](https://qiita-image-store.s3.amazonaws.com/0/135794/6648b9ce-e804-b32b-cf74-de4c012708ca.gif)



# Step1

`UITextViewDelegate`をデリゲート


```swift
class MyViewController: UIViewController, UITextViewDelegate {
```

# step2

`viewWillAppear`に下記のように書く `bodyTextView`は自分の`IBOutlet`です

```swift
 
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        
        
        bodyTextView.text = "Placeholder"
        bodyTextView.textColor = UIColor.lightGray
        
        
    }

```


# step3

`textViewDidBeginEditing`と`textViewDidEndEditing`を実装


```swift
 
    
    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` をデリゲート

```swift 
class MyViewController: UIViewController, UITextFieldDelegate, UITextViewDelegate {

```
# step5

`viewWillAppear`に下記のように書く `titleTextField`は自分の`IBOutlet`です

```swift
override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        
        
        bodyTextView.text = "Placeholder"
        bodyTextView.textColor = UIColor.lightGray
        
        titleTextField.text = "Placeholder"
        titleTextField.textColor = UIColor.lightGray
        
        
    }

```

# step6

`textFieldDidBeginEditing`と`textViewDidEndEditing`を実装


```swift

      /*
     
     テキストフィールドもプレイスフォルダー実装
     
     */
    
    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を使わずに実装する方法](http://off.tokyo/blog/2017/05/05/swift3-uitextview%E3%81%ABplaceholder%E3%82%92label%E3%82%92%E4%BD%BF%E3%82%8F%E3%81%9A%E3%81%AB%E5%AE%9F%E8%A3%85%E3%81%99%E3%82%8B%E6%96%B9%E6%B3%95/)