I have an app which has chatting functionality where UITextview
is used for entering the message. UITextview
height has to be dynamic (if user enters the message, the height has to be changed according to the text length till a specific Height).
How can I achieve this?
Disable Scrolling of textView.
TO Increase Height to a specific value and then enable scrolling.
Provide a maximum height constraint then add this code to your viewController
class YourViewController: UIViewController, UITextViewDelegate
{
@IBOutlet weak var yourTextView: UITextView!
let textViewMaxHeight: CGFloat = 100
override func viewDidLoad()
{
super.viewDidLoad()
yourTextView.delegate = self
}
func textViewDidChange(textView: UITextView)
{
if textView.contentSize.height >= self.textViewMaxHeight
{
textView.scrollEnabled = true
}
else
{
textView.frame.size.height = textView.contentSize.height
textView.scrollEnabled = false
}
}
}