how to make UITextView height dynamic according to text length?

前端 未结 21 2232
隐瞒了意图╮
隐瞒了意图╮ 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:09

    If your textView is allowed to grow as tall as the content, then

    textView.isScrollEnabled = false
    

    should just work with autolayout.

    If you want to remain the textView to be scrollable, you need to add an optional height constraint,

    internal lazy var textViewHeightConstraint: NSLayoutConstraint = {
      let constraint = self.textView.heightAnchor.constraint(equalToConstant: 0)
      constraint.priority = .defaultHigh
      return constraint
    }()
    
    public override func layoutSubviews() {
      super.layoutSubviews()
    
      // Assuming there is width constraint setup on the textView.
      let targetSize = CGSize(width: textView.frame.width, height: CGFloat(MAXFLOAT))
      textViewHeightConstraint.constant = textView.sizeThatFits(targetSize).height
    }
    

    The reason to override layoutSubviews() is to make sure the textView is laid out properly horizontally so we can rely on the width to calculate the height.

    Since the height constraint is set to a lower priority, if it runs out space vertically the actual height of the textView will be less than the contentSize. And the textView will be scrollable.

    0 讨论(0)
  • 2020-11-28 06:10

    Declaration here

        fileprivate weak var textView: UITextView!
    

    Call your setupview here

        override func viewDidLoad() {
            super.viewDidLoad()
    
             setupViews()
        }
    

    Setup here

        fileprivate func setupViews() {
    
        let textView = UITextView()
        textView.translatesAutoresizingMaskIntoConstraints = false
        textView.text = "your text here"
        textView.font = UIFont.poppinsMedium(size: 14)
        textView.textColor = UIColor.brownishGrey
        textView.textAlignment = .left
        textView.isEditable = false
        textView.isScrollEnabled = false
        textView.textContainerInset = UIEdgeInsets(top: 20, left: 20, bottom: 20, right: 20)
    
       self.view.addSubview(textView)
    
       self.textView = textView
    
       setupConstraints()
    
       }
    

    Setup constraints here

       fileprivate func setupConstraints() {
    
        NSLayoutConstraint.activate([
    
            textView.topAnchor.constraint(equalTo: view.topAnchor, constant: 20),
            textView.leftAnchor.constraint(equalTo: view.leftAnchor, constant: 20),
            textView.rightAnchor.constraint(equalTo: view.rightAnchor, constant: -20),
            textView.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: -20),
            textView.heightAnchor.constraint(greaterThanOrEqualToConstant: 150),
            ])
      }
    
    0 讨论(0)
  • 2020-11-28 06:12

    Followed by DeyaEldeen's answer.
    In my case. I grow the textview height automatically by adding

    swift 3

    textView.translatesAutoresizingMaskIntoConstraints = false textView.isScrollEnabled = false

    0 讨论(0)
  • 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
         }
    }
    
    0 讨论(0)
  • 2020-11-28 06:14

    Tested on Xcode 11.2.1, Swift 5.1:

    All I had to do was:

    1. Set the constraints to the top, left, and right of the textView.
    2. Disable scrolling in Storyboard.

    Doing so allowed autolayout to kick in and dynamically size the textView based on its content.

    0 讨论(0)
  • 2020-11-28 06:14

    Better yet swift 4 add as an extension:

    extension UITextView {
        func resizeForHeight(){
            self.translatesAutoresizingMaskIntoConstraints = true
            self.sizeToFit()
            self.isScrollEnabled = false
        }
    }
    
    0 讨论(0)
提交回复
热议问题