Is there a good way to adjust the size of a UITextView
to conform to its content? Say for instance I have a UITextView
that contains one line of text:
"Hello world"
I then add another line of text:
"Goodbye world"
Is there a good way in Cocoa Touch to get the rect
that will hold all of the lines in the text view so that I can adjust the parent view accordingly?
As another example, look at the notes' field for events in the Calendar application - note how the cell (and the UITextView
it contains) expands to hold all lines of text in the notes' string.
Guys using autolayout and your sizetofit isn't working, then please check your width constraint once. If you had missed the width constraint then the height will be accurate.
No need to use any other API. just one line would fix all the issue.
Here, I was only concerned with height, keeping the width fixed and had missed the width constraint of my TextView in storyboard.
And this was to show up the dynamic content from the services.
Hope this might help..
if any other get here, this solution work for me, 1"Ronnie Liew"+4"user63934" (My text arrive from web service): note the 1000 (nothing can be so big "in my case")
And that is... Cheers
disable scrolling
add constaints
and add your text
if you use
UIScrollView
you should add this too;Here is the answer if you need resize
textView
andtableViewCell
dynamically instaticTableView
[https://stackoverflow.com/a/43137182/5360675][1]
Very easy working solution using code and storyboard both.
By Code
By Storyboard
Uncheck the Scrolling Enable
No need to do anything apart of this.
The only code that will work is the one that uses 'SizeToFit' as in jhibberd answer above but actually it won't pick up unless you call it in ViewDidAppear or wire it to UITextView text changed event.