1

I have a UITextView which I would like to dynamically resize dependant on its content.

For some reason the textview is always shorter that its contents! Here's the code:

_textView.text = [NSString stringWithFormat:@"Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test "];

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

And here's the result:

UITextView Too Short

As you can see the UITextView (in white) is too short. If I put more or less contents in there the shortness is the same, i.e.

More TextLess Text

I'm sure it's something simple but I can't work it out.

Leon
  • 3,614
  • 1
  • 33
  • 46
  • Have a look here : http://stackoverflow.com/questions/19028743/ios7-uitextview-contentsize-height-alternative/19067476#19067476 – Jordan Montel Oct 09 '13 at 15:32

3 Answers3

0

add the UITextView to the main view (addSubview) before resizing its frame (see this question)

Community
  • 1
  • 1
NightCoder
  • 1,049
  • 14
  • 22
0

I found the answer here: https://stackoverflow.com/a/2487402/255463, then I do this:

First, I add a UITextView using the storyboard, then I embed the textView inside a UIScrollView, using the storyboard too, then connect each view with an outlet with the storyboard. Inside the view controller sublass this what I got:

#import "DetailViewController.h"

@interface DetailViewController ()

@property (weak, nonatomic) IBOutlet UITextView *textView;
@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;

@end

@implementation DetailViewController

@synthesize textView = _textView;
@synthesize scrollView = _scrollView;

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.textView.text = [NSString stringWithFormat:@"Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test "];
    CGRect frame = self.textView.frame;
    frame.size.height = self.textView.contentSize.height;
    self.textView.frame = frame;

    self.scrollView.contentSize = CGSizeMake(320.0, self.descriptionView.contentSize.height + 147.0);
}

@end
Community
  • 1
  • 1
nebiros
  • 717
  • 10
  • 30
  • This is basically what I was doing yeah. – Leon Nov 15 '12 at 18:45
  • I believe that, I have just tried again using your code and it does the same thing. Really confused! – Leon Nov 27 '12 at 17:45
  • Unfortunately in iOS7 the `contentSize` property of UITextView is not updated immediately when the text is changed. I am developing a workaround. – phatmann Sep 16 '13 at 20:47
0

I had this same problem using storyboards in iOS7.

My solution was to add this code:

    CGRect newTextViewFrame = self.textView.frame;
    newTextViewFrame.size.width = self.textView.contentSize.width + self.textView.contentInset.right + self.textView.contentInset.left;
    newTextViewFrame.size.height = self.textView.contentSize.height + self.textView.contentInset.top + self.textView.contentInset.bottom;
    self.textView.frame = newTextViewFrame;

in the viewDidAppear: view life cycle method and it was immediately sized to fit it's contents. Unfortunately, this resize happens after the view has already appeared which means that the user sees it happening.

Example:

- (void)viewDidAppear:(BOOL)animated
{
        [super viewDidAppear:animated];

        CGRect newTextViewFrame = self.textView.frame;
        newTextViewFrame.size.width = self.textView.contentSize.width + self.textView.contentInset.right + self.textView.contentInset.left;
        newTextViewFrame.size.height = self.textView.contentSize.height + self.textView.contentInset.top + self.textView.contentInset.bottom;
        self.textView.frame = newTextViewFrame;
}
Jonathan
  • 2,623
  • 3
  • 23
  • 38
  • I have moved on from this problem as I ended up using a UIWebView instead (for other reasons). I will make this as answered though, thanks. – Leon Oct 09 '13 at 16:06