10

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

bryanmac
  • 38,941
  • 11
  • 91
  • 99
  • 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 Answers1

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