views:

1567

answers:

2

Hello guys, I already did several searches on Stack Overflow and Google, but I couldn't find a solution to my problem.

I have a Detail View for a product that includes a UIScrollView and a few subviews (UIImageView, UILabel, UITextView). You can find an example here.

First I wanna autoresize the UITextView (not the text itself!) to the corresponding height. Then I wanna autoresize the entire UIScrollView so that it fits the height of my UITextView and the elements above it. I've tried the following code:

myUITextView.frame = CGRectMake(2.0, 98.0, 316.0, self.view.bounds.size.height);

[scrollView setContentOffset:CGPointMake(0.0, 0.0) animated:NO];
scrollView.contentSize = CGSizeMake(320.0, 98.0 + [myUITextView frame].size.height);

[self.view addSubview:scrollView];

98.0 + [myUITextView frame].size.height) because my thought was: After getting the height of myUITextView, add the height of the other subviews (98.0) to it.

Unfortunately it doesn't work very well. Depending on the content of the UIScrollView, it is too short or too long. I did some logging with the result that the height of the UITextView is always the same:

2010-01-27 14:15:45.096 myApp[1362:207] [myUITextView frame].size.height: 367.000000

Thanks!

A: 

Hi.

A UITextView won't automatically resize itself to it's contents (not as far as I know anyway) so you need to get the size of the text yourself and size the UIView accordingly.

The functions on this page will help - you can use them to get the width and height of a string, something like

// Get the size that the string will render at
NSString *contents = @"This is the content of your UITextView";
CGSize areaSize = [contents sizeWithFont:myView.font forWidth:myView.frame.size.width lineBreakMode:UILineBreakModeWordWrap];

// Then set the frame of your UITextView to be the right size
myView.bounds = CGRectMake(0, 0, areaSize.width, areaSize.height);

Then, you can layout the other components around it.

Hope this helps,

S

PS Warning, the link to the docs is correct but my code example is untested, sorry :)

deanWombourne
Thanks for your help.It seems sizeWithFont:forWidth:lineBreakMode: only works for a single line of text. I've tried sizeWithFont:constrainedToSize:lineBreakMode: and it kinda works. Sometimes it doesn't calculate the correct height. I guess it's because of some line breaks in my text source so I've added 30 px to the button.
dennis3484
I've just checked the docs and you're absolutely right, sorry about that! I've had problems with it sometimes returning the wrong height too and I used exactly the same solution you did - add 30px :)
deanWombourne
+1  A: 

There is actually a very easy way to do resize the UITextView to its correct height using its contentSize.

CGRect frame = _textView.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. Prior to that it is equal to frame.size

Ronnie Liew