Placeholder in UITextView

前端 未结 30 3084
野趣味
野趣味 2020-11-22 16:01

My application uses an UITextView. Now I want the UITextView to have a placeholder similar to the one you can set for an UITextField.<

30条回答
  •  执念已碎
    2020-11-22 16:08

    Simple Swift 3 solution

    Add UITextViewDelegate to your class

    Set yourTextView.delegate = self

    Create placeholderLabel and position it inside yourTextView

    Now just animate placeholderLabel.alpha on textViewDidChange:

      func textViewDidChange(_ textView: UITextView) {
        let newAlpha: CGFloat = textView.text.isEmpty ? 1 : 0
        if placeholderLabel.alpha != newAlpha {
          UIView.animate(withDuration: 0.3) {
            self.placeholderLabel.alpha = newAlpha
          }
        }
      }
    

    you might have to play with placeholderLabel position to set it up right, but that shouldn't be too hard

提交回复
热议问题