Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to make UITextView height dynamic according to text length?

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 text length.

*I saw other questions, but solutions there didn't work for me

example

like image 647
DeyaEldeen Avatar asked Aug 02 '16 07:08

DeyaEldeen


People also ask

How do I size a UITextView to its content?

It can be done using the UITextView contentSize . This will not work if auto layout is ON. With auto layout, the general approach is to use the sizeThatFits method and update the constant value on a height constraint. CGSize sizeThatShouldFitTheContent = [_textView sizeThatFits:_textView.

How do I increase the height of a TextField in Swift?

Step 1 : Click the Attribute Inspector, Select the Border Styles which is not the rounded one. Step 2 : Now go to Size Inspector and change the size of the TextField. Step 3 : Create an @IBOutlet for TextField and then add below code inside the viewWillAppear() or viewDidAppear() .


2 Answers

this Works for me, all other solutions didn't.

func adjustUITextViewHeight(arg : UITextView) {     arg.translatesAutoresizingMaskIntoConstraints = true     arg.sizeToFit()     arg.scrollEnabled = false } 

In Swift 4 the syntax of arg.scrollEnabled = false has changed to arg.isScrollEnabled = false.

like image 148
DeyaEldeen Avatar answered Sep 19 '22 18:09

DeyaEldeen


In Storyboard / Interface Builder simply disable scrolling in the Attribute inspector.

In code textField.scrollEnabled = false should do the trick.

like image 33
Tum Avatar answered Sep 17 '22 18:09

Tum