0

i have app that have table with many cells, when user tap on it, then detail view controller appear. Detail View Controller have an image and UITextView, that is not editable and it purpose only for showing text (just like label). Text it contain come from an xml file, sometimes it have several rows, sometimes there is a lot of text.

What i want is - to make UITextView height change depending of amount of text it have. For example, for several rows it might be 50 pixels height, in other case 300 pixels. How could i measure correct height and set it for UITextView? Again i want to point that it purpose only for showing text, not edit in any way. My second task is to measure final UITextView height and store it in some variable.

And there is what i've tried:

CGRect textViewRect      = self.myTextView.frame;
textViewRect.size.height = self.myTextView.contentSize.height;
self.myTextView.frame = textViewRect;

But apparently its not working. Any idea how to achieve that?

Any advice would be appreciated, thanks!

Surfer
  • 1,370
  • 3
  • 19
  • 34
Evgeniy Kleban
  • 6,794
  • 13
  • 54
  • 107
  • Duplicate of http://stackoverflow.com/questions/19028743/ios7-uitextview-contentsize-height-alternative – iPatel Mar 15 '14 at 08:51

2 Answers2

1

Use this method of UIView:

- (CGSize)sizeThatFits:(CGSize)size

Typically you want your view limited in width, but any height. So you pass in a size with width as much as you want, and height for example 10000.0, so it gets as high as it needs. This returns how big the view needs to be according to its current content.

gnasher729
  • 51,477
  • 5
  • 75
  • 98
1

Try this

UITextView *textview1;
[textview1.text sizeWithFont:text.font constrainedToSize:textview1.frame.size];
Indrajeet
  • 5,490
  • 2
  • 28
  • 43