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.
Swift :
This worked nicely when I needed to make text in a
UITextView
fit a specific area:here is the swift version of @jhibberd
Did you try
[textView sizeThatFits:textView.bounds]
?Edit: sizeThatFits returns the size but does not actually resize the component. I'm not sure if that's what you want, or if
[textView sizeToFit]
is more what you were looking for. In either case, I do not know if it will perfectly fit the content like you want, but it's the first thing to try.For iOS 7.0, instead of setting the
frame.size.height
to thecontentSize.height
(which currently does nothing) use[textView sizeToFit]
.See this question.
For those who want the textview to actually move up and maintain the bottom line position