環境
- 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
}
}
}
実行
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
textViewDidBeginEditing
とtextViewDidEndEditing
を実装
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
textFieldDidBeginEditing
とtextViewDidEndEditing
を実装
/*
テキストフィールドもプレイスフォルダー実装
*/
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
}
}