Take the 2-minute tour ×

I am having issue in getting the correct size of uitextview.In my application i am fetching the data from webservice and i set the height of uitextview as per the data from webservice .But whenever there is space in first 35 letters(frame of my textview is (20,0,255,2000)). then the hjeight calculated is always one line less then correct height and even the uitext view starts to print the line after space in the new line (may be word is big thats why ??).

So i am getting the wrong text view height.

The text view apears somwethibng like this :

firstword spacesspacesspacesspaces spacesspacesspacesspaces spacesspacesspacesspaces spacesspacesspacesspaces spacesspacesspacesspaces

When i enter text which is say without spaces it gives me correct height suppose say the data is "ttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttt" Then in the above scenerio Cgsize will generate proper result for me .

Here i am not using UILabel is because i want link detection thats why . If anyone has anyother suggestions it is highly welcomed.

Regards Mrugen

share|improve this question
add comment

3 Answers

up vote 22 down vote accepted

Using NSString's sizeWithFont methods doesn't really work for a text view. It works great for a UILabel, but text views seem to have extra padding around the text. If you use that method, you end up with a size that's a little short.

UITextView is a subclass of UIScrollView. After you set the textview's text property, its contentSize property is automatically updated to fit the text. So, if you want to set the text view's height to fit its text just do:

textView.text    = @"some text";
CGRect rect      = textView.frame;
rect.size.height = textView.contentSize.height;
textView.frame   = rect;

That will set your textView's hight so it contains the text.

share|improve this answer
@ charlie here in additon to this we alos need to set the textview setFitTOsize and it did for me .Thanks –  mrugen Apr 13 '11 at 6:29
Not sure where to find the setFitToSize method, simply setting the textView's text and then updating the height with the content size didn't change the height. –  user441669 Oct 8 '13 at 19:50
add comment

You can measure the height of the text with this function and set your frame according to the returned height.

   -(CGSize)sizeOfText:(NSString *)textToMesure widthOfTextView:(CGFloat)width withFont:(UIFont*)font
        CGSize ts = [textToMesure sizeWithFont:font constrainedToSize:CGSizeMake(width-20.0, FLT_MAX) lineBreakMode:UILineBreakModeWordWrap];
        return ts; 

you use it like that:

 CGSize size =   [self sizeOfText:@"the string you want to present" widthOfTextView:255 withFont:fontYouAreUsing];

And then you pass it to the text view:

   textView.frame = CGRectMake(20,0,255,size.height);

Good luck


share|improve this answer
With iOS 7 contentSize for textView is not working and your solution is needed a lot but xCode says sizeWithFont is deprecated. What's you suggestion now? I need your help. Thanks –  Elias Naranjee Sani Oct 2 '13 at 8:27
add comment

The answer for your question is Create an instance for UITextView,In my case I have created as txtView and then add text to it and print out the height in content height in console.

NSLog(@"TextView Height is :%d",txtView.contentSize.height)

share|improve this answer
that has to be the least dynamic approach i have ever heard of. –  katzenhut Oct 11 '13 at 10:12
add comment

Your Answer


