Axel B
Axel B

Reputation: 1

Resizing UITextView automatically

I'm a beginner in iOS development, and I have trouble resizing automatically a UITextView.

I created a Master-Detail Application. On my DetailViewController.xib I added a ScrollView that encompass a Label for the title, an image and under this image, a TextView in order to put a description that the user posted.
The problem is that the description depends on what my WebService returns, so I need it to be resized automatically.

Here's my code:

- (void)configureView
{
    ...
    self.detailDescriptionTextView.text = [self.detailItem valueForKey:@"adDescription"];

    CGRect frame = self.detailDescriptionTextView.frame;
    frame.size.height = self.detailDescriptionTextView.contentSize.height;
    self.detailDescriptionTextView.frame = frame;
)

The problem is that the TextView gets the content correctly but it doesn't resize at all.. I looked for an answer for a while and most of answers are what I tried...

Thanks for your help.

EDIT : I realized something. In order to put some elements under the description, I've put a very small TextView for the description in the Interface Builder, is it a problem? If it is, how could I put other elements under this TextView, because there would be no more space on the Interface Builder no?

EDIT 2 : I finally succeeded. It seems like the problem was that I created the TextView by Interface Builder. By creating it programmatically, it worked perfectly. Here's my code if it can help someone:

UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(20, 195, 280, 10)];
textView.text = [self.detailItem valueForKey:@"adDescription"];
textView.font = [UIFont fontWithName:@"Hevlatica" size:14];
[self.scrollView addSubview:textView];
CGRect descriptionFrame = textView.frame;
descriptionFrame.size.height = textView.contentSize.height;
textView.frame = descriptionFrame;

Thanks for your help.

Upvotes: 1

Views: 7895

Answers (2)

Manu
Manu

Reputation: 4750

 CGSize constraint = CGSizeMake(690.0, 2000.0);
 int h=10;
 CGSize size_txt_overview1 = [[self.detailItem valueForKey:@"adDescription"] sizeWithFont:[UIFont fontWithName:@"Helvetica" size:18] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];
frame.size.height = size_txt_overview1.height;
self.detailDescriptionTextView.frame = frame;

Upvotes: 1

arun.s
arun.s

Reputation: 1528

First calculate the content height using

NSString *content = @"Hello how are you.";
CGSize size = [content sizeWithFont:[UIFont systemFontOfSize:14]
              constrainedToSize:CGSizeMake(yourWidth, MAX_HEIGHT)
                  lineBreakMode:UILineBreakModeWordWrap];

and then set the frame of textView

[self.textView setFrame:CGRectMake(5, 30, 100, size.height + 10)];

Or

Add the content to your textview and then try this

CGRect frame = self.textView.frame;
frame.size.height = self.textView.contentSize.height;
self.textView.frame = frame;

Hope this Helps !!!

Upvotes: 4

Related Questions