Swift の UITextView 内にプレースホルダーテキストを追加しますか? 質問する

Swift の UITextView 内にプレースホルダーテキストを追加しますか? 質問する

で にUITextView設定できるものと同様のプレースホルダーを に追加するにはどうすればよいですか?UITextFieldSwift

ベストアンサー1

Swift 4 用に更新

UITextView本質的にプレースホルダー プロパティがないため、UITextViewDelegateメソッドを使用してプログラムでプレースホルダー プロパティを作成し、操作する必要があります。目的の動作に応じて、以下のソリューション 1 または 2 のいずれかを使用することをお勧めします。

注: どちらのソリューションでも、UITextViewDelegateクラスに追加し、textView.delegate = selfテキスト ビューのデリゲート メソッドを使用するように設定します。


解決策 1 - ユーザーがテキスト ビューを選択するとすぐにプレースホルダーが消えるようにしたい場合:

まず、 にプレースホルダー テキストを含めるように設定し、のプレースホルダー テキストUITextViewの外観を模倣するために、 を薄い灰色に設定します。 で、またはテキスト ビューの作成時にこれを行います。UITextFieldviewDidLoad

textView.text = "Placeholder"
textView.textColor = UIColor.lightGray

次に、ユーザーがテキスト ビューの編集を開始するときに、テキスト ビューにプレースホルダーが含まれている場合 (つまり、テキストの色が薄い灰色の場合)、プレースホルダー テキストをクリアし、ユーザーの入力に対応するためにテキストの色を黒に設定します。

func textViewDidBeginEditing(_ textView: UITextView) {
    if textView.textColor == UIColor.lightGray {
        textView.text = nil
        textView.textColor = UIColor.black
    }
}

次に、ユーザーがテキスト ビューの編集を終了し、テキスト ビューがファースト レスポンダーとして再始動したときに、テキスト ビューが空の場合は、プレースホルダー テキストを再度追加し、その色を薄い灰色に設定して、プレースホルダーをリセットします。

func textViewDidEndEditing(_ textView: UITextView) {
    if textView.text.isEmpty {
        textView.text = "Placeholder"
        textView.textColor = UIColor.lightGray
    }
}

解決策 2 - テキスト ビューが選択されている場合でも、テキスト ビューが空のときにプレースホルダーを表示する場合:

まず、プレースホルダーを設定しますviewDidLoad:

textView.text = "Placeholder"
textView.textColor = UIColor.lightGray

textView.becomeFirstResponder()

textView.selectedTextRange = textView.textRange(from: textView.beginningOfDocument, to: textView.beginningOfDocument)

(注: OP はビューが読み込まれるとすぐにテキスト ビューが選択されるようにしたかったので、上記のコードにテキスト ビューの選択を組み込みました。これが希望する動作ではなく、ビューの読み込み時にテキスト ビューを選択したくない場合は、上記のコード チャンクから最後の 2 行を削除してください。)

次に、shouldChangeTextInRange UITextViewDelegate次のようにメソッドを使用します。

func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {

    // Combine the textView text and the replacement text to
    // create the updated text string
    let currentText:String = textView.text
    let updatedText = (currentText as NSString).replacingCharacters(in: range, with: text)

    // If updated text view will be empty, add the placeholder
    // and set the cursor to the beginning of the text view
    if updatedText.isEmpty {

        textView.text = "Placeholder"
        textView.textColor = UIColor.lightGray

        textView.selectedTextRange = textView.textRange(from: textView.beginningOfDocument, to: textView.beginningOfDocument)
    }

    // Else if the text view's placeholder is showing and the
    // length of the replacement string is greater than 0, set 
    // the text color to black then set its text to the
    // replacement string
     else if textView.textColor == UIColor.lightGray && !text.isEmpty {
        textView.textColor = UIColor.black
        textView.text = text
    }

    // For every other case, the text should change with the usual
    // behavior...
    else {
        return true
    }

    // ...otherwise return false since the updates have already
    // been made
    return false
}

また、textViewDidChangeSelectionプレースホルダーが表示されている間にユーザーがカーソルの位置を変更できないように実装します。(注:textViewDidChangeSelectionビューが読み込まれる前に呼び出されるため、ウィンドウが表示されている場合にのみテキスト ビューの色をチェックします)。

func textViewDidChangeSelection(_ textView: UITextView) {
    if self.view.window != nil {
        if textView.textColor == UIColor.lightGray {
            textView.selectedTextRange = textView.textRange(from: textView.beginningOfDocument, to: textView.beginningOfDocument)
        }
    }
}

おすすめ記事