How do I make a UITextView expand or shrink to the size of the text inside it and then make the UIScrollview Expand or shrink to fit the UITextView
Asked
Active
Viewed 2.4k times
10
-
It seems like you should be able to do what you're trying to do using -[UIView sizeToFit]. Take a look at the docs for the UIView class and see if that's what you're looking for. Some sample code would help to clarify exactly what you're attempting. – Mike Hay Aug 30 '11 at 01:36
-
Please refer to this link: https://stackoverflow.com/a/38418756/3024579 – Alok Apr 24 '19 at 09:31
1 Answers
28
CGRect frame;
frame = textiew.frame;
frame.size.height = [textView contentSize].height;
textView.frame = frame;
One thing to note is that the correct contentSize is only available after the UITextView has been added to the view with addSubview and the text is assigned to it. Prior to that it is equal to frame.size

Suresh Varma
- 9,750
- 1
- 60
- 91

bryanmac
- 38,941
- 11
- 91
- 99
-
8
-
Does that simplify to `cell.commentLabel.frame.size.height = cell.commentLabel.contentSize.height` in Swift? – User Sep 28 '14 at 16:56
-
@Darksinge your effort to update code to Swift 2.0 is appreciate, but you need to post it as your own answer. – Petter Friberg Jan 28 '16 at 22:01