Navya
Navya

Reputation: 39

UITextview height vary dynamically based on content

I am new in iOS.

My task is to send feedback data from feedback screen.

In Feedback screen I have 3 fields:

which were presented in a UIView.

Based on the content in feedback textview, the height of textview should increase as well as when ever the textview height increasing or decreasing, the UIView height also vary.

The main thing is, the textview should increase double of it's height and after that scroll action should perform.

I am using Auto-layouts to achieve this.If any one can helps me to achieve this would be great.Thank in advance.

Code ref:

import UIKit

class FeedbackViewController: UIViewController {

    @IBOutlet weak var feedbacktextview: UITextView!

    @IBOutlet weak var textviewheight: NSLayoutConstraint!


  @IBOutlet weak var containerviewheight: NSLayoutConstraint!
    override func viewDidLoad() {
        super.viewDidLoad()
 self.navigationController?.setNavigationBarHidden(true, animated: false)

    }

}
extension FeedbackViewController: UITextViewDelegate{
    override func didChangeValue(forKey key: String) {
        textviewheight.constant = feedbacktextview.contentSize.height
        containerviewheight.constant = feedbacktextview.frame.size.height
//        let fixedWidth = feedbacktextview.frame.size.width
//        let newSize = feedbacktextview.sizeThatFits(CGSize(width: fixedWidth, height: CGFloat.greatestFiniteMagnitude))
//        feedbacktextview.frame.size = CGSize(width: max(newSize.width, fixedWidth), height: newSize.height)

    }
}

Upvotes: 0

Views: 76

Answers (3)

Parth Patel
Parth Patel

Reputation: 919

Try this

@IBOutlet weak var txtView: UITextView!
@IBOutlet weak var constLocationHeight: NSLayoutConstraint!

func textViewDidChange(_ textView: UITextView) {
     adjustFrames(textView, constLocationHeight)        
}

func adjustFrames(_ textView: UITextView, _ constTextview: NSLayoutConstraint)  {
        if textView.contentSize.height >= 80 {
            constTextview.constant = 80
            return;
        } else if textView.contentSize.height < 30 {
            constTextview.constant = 33
            return
        }
        constTextview.constant = textView.contentSize.height
        textView.beginFloatingCursor(at: CGPoint.zero)
        textView.endFloatingCursor()
}

Upvotes: 0

Hairsh Kuramsetty
Hairsh Kuramsetty

Reputation: 101

Create Outlet to the textView and Make sure you select scrollEnabled to no

textview.scrollEnabled = false

let fixedWidth = textView.frame.size.width
let newSize = textView.sizeThatFits(CGSize(width: fixedWidth, height: CGFloat.greatestFiniteMagnitude))
textView.frame.size = CGSize(width: max(newSize.width, fixedWidth), height: newSize.height)

Upvotes: 1

Nick
Nick

Reputation: 875

Try this:

First create an @IBOutlet for the UITextView and the height constraint. And then after dynamically change the height in code:

class ViewControler: UIViewController, UITextViewDelegate {

    @IBOutlet weak var txtView: UITextView!
    @IBOutlet weak var txtViewHeight: NSLayoutConstraint!


    override func viewDidLoad() {
        super.viewDidLoad()
        self.txtView.delegate = self
    }

    func textViewDidChange(textView: UITextView) {
        let sizeToFitIn = CGSizeMake(self.txtView.bounds.size.width, CGFloat(MAXFLOAT))
        let newSize = self.txtView.sizeThatFits(sizeToFitIn)
        self.txtViewHeight.constant = newSize.height
    }

}

Upvotes: 0

Related Questions