Tidane
Tidane

Reputation: 694

How to resize a UITextView in a Cell when changing from Portrait to Landscape?

I have a TextView made by code.

CGRect textFrame = CGRectMake(5, 20, 300, 50);

UITextView *textView = [[UITextView alloc] initWithFrame:textFrame];

The problem is, i want to resize it when landscape mode is called.

Here are some examples:

https://docs.google.com/file/d/0BzsX_FHtJa9nSjRVamtFX1pjdlE/edit?usp=sharing

https://docs.google.com/file/d/0BzsX_FHtJa9nU0U5T3JvZkgzZWc/edit?usp=sharing

Thanks

PS: This is just for iPhone. No iPad implementation.

Edit:

TableView code:

tableProducts.dataSource = self;
tableProducts.delegate = self;
tableProducts.bounces = YES;

Cell code:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableProducts dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;        
}

/* ------ Product title in RED ------*/

CGRect titleFrame = CGRectMake(5, 5, 250, 15);
UILabel *productTitle = [[UILabel alloc] initWithFrame:titleFrame];
productTitle.font = [UIFont boldSystemFontOfSize:14];
productTitle.textColor = [UIColor redColor];
productTitle.text = [listProducts objectAtIndex:indexPath.row];

[cell.contentView addSubview:productTitle];

/* ------ Product title in RED ------*/

/* ------ Product Text ------*/

CGRect textFrame = CGRectMake(5, 20, 300, 50);
UITextView *textView = [[UITextView alloc] initWithFrame:textFrame];
textView.textColor = [UIColor blackColor];
textView.font = [UIFont boldSystemFontOfSize:11.5];
textView.text = [listTexts objectAtIndex:indexPath.row];
textView.scrollEnabled = NO;
textView.editable = NO;

[cell.contentView addSubview:textView];

/* ------ Product Text ------*/

return cell;
}

Upvotes: 1

Views: 316

Answers (2)

Tommy Devoy
Tommy Devoy

Reputation: 13549

Just change the frame whenever you switch orientations. Return yes for shouldAutoRotate and then in willAnimateRotation just do something like this:

-(void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
    if(UIInterfaceOrientationIsLandscape(toInterfaceOrientation))
    {
       textView.frame = CGRectMake(x,y,width,height)//whatever values you want for landscape
    }
    else
    {
       textView.frame = CGRectMake(x,y,width,height)//whatever values you want for portrait
    }
}

Upvotes: 1

JMD
JMD

Reputation: 1590

set your text's autoresizing mask

 textview.autoresizingMask = UIViewAutoresizingFlexibleWidth;

Upvotes: 1

Related Questions