LoginSignup
34
32

More than 5 years have passed since last update.

UITextViewにてPlaceholderを実装する

Posted at

UITextViewにはUITextFieldのようにPlaceholderが設定できませんので、自作する必要があります。まず、UITextViewを作り、その上にLabelを置きます。TextViewに入力されているかどうかで、このLabelを非表示にすれば、Placeholderとして動きます。

label.png

    //textviewがフォーカスされたら、Labelを非表示
    func textViewShouldBeginEditing(textView: UITextView) -> Bool
    {
        lbl.hidden = true
        return true
    }

    //textviewからフォーカスが外れて、TextViewが空だったらLabelを再び表示
    func textViewDidEndEditing(textView: UITextView) {

        if(textView.text.isEmpty){
            lbl.hidden = false
        }
    }
34
32
0

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
34
32