how to make UITextView height dynamic according to text length?

前端 未结 21 2214
隐瞒了意图╮
隐瞒了意图╮ 2020-11-28 05:44

As you can see in this image

the UITextView changes it\'s height according to the text length, I want to make it adjust it\'s height according to the te

21条回答
  •  有刺的猬
    2020-11-28 06:13

    In my project, the view controller is involved with lots of Constraints and StackView, and I set the TextView height as a constraint, and it varies based on the textView.contentSize.height value.

    step1: get a IB outlet

    @IBOutlet weak var textViewHeight: NSLayoutConstraint!
    

    step2: use the delegation method below.

    extension NewPostViewController: UITextViewDelegate {
         func textViewDidChange(_ textView: UITextView) {
              textViewHeight.constant = self.textView.contentSize.height + 10
         }
    }
    

提交回复
热议问题