Thursday, October 3, 2013

iOS 7 (XCode 5) Tutorial - ContentSize of Text View is not calculating correctly any more with iOS 7

iOS 7 (XCode 5) ContentSize of Text View is not calculating correctly any more with iOS 7


Before X Code 5, to get the contents height of text view we use text view's property contentSize. But its no longer work with new iOS 7. 

Option 1:

With iOS 7, we have a different property named textContainer. It gives text container of text view. 

You need to replace the following line of code (These line of code set the text view frame according to its content length.)

 CGRect frame = _textView.frame;
 frame.size.height = _textView.contentSize.height;
_textView.frame = frame;

with 

CGRect frame = _textView.frame;
 frame.size.height = _textView.textContainer.size.height;
 _textView.frame = frame;

_textView.textContainer.size gives the same value which was given by _textView.contentSize earlier.

Option 2:

We can also replace the line of code 


 CGRect frame = _textView.frame;
 frame.size.height = _textView.contentSize.height;
_textView.frame = frame;

with 

CGRect frame = _textView.frame;
 frame.size.height = [_textView sizeThatFits:CGSizeMake(txtView.frame.size.width, MAXFLOAT)].height;
 _textView.frame = frame;

Above lines of code will work with each iOS.