GAMA
GAMA

Reputation: 5996

not able to increase width of UILabel in landscape mode

I had made an iPad application, in that I have tableView whose name is crollTableView, in my tableView I am creating two labels in each cell of row, whose name is capitalLabel and stateLabel, in portrait mode,

now I want to increase width of my labels in landscape mode,

here is code snippet,

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

if(tableView==crollTableView){
        static NSString *CellIdentifier=@"Cell";
        static NSInteger StateTag = 1;
        static NSInteger CapitalTag = 2;
        UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if(cell == nil){

            // cell.backgroundColor=[UIColor yellowColor];
            cell=[[[UITableViewCell alloc]initWithFrame:CGRectZero reuseIdentifier:CellIdentifier]autorelease];
            CGRect frame;
            frame.origin.x = 0; 
            frame.origin.y = 0;
            frame.size.height = 70;
            frame.size.width = 650;


            UILabel *capitalLabel = [[UILabel alloc] initWithFrame:frame];
            capitalLabel.tag = CapitalTag;
            capitalLabel.opaque = FALSE;
            capitalLabel.font=[UIFont systemFontOfSize:20.0];
            [cell.contentView addSubview:capitalLabel];


            frame.origin.x += 680;
            UILabel *stateLabel = [[UILabel alloc] initWithFrame:frame];
            stateLabel.tag = StateTag;
            stateLabel.font=[UIFont systemFontOfSize:17.0];
            [cell.contentView addSubview:stateLabel];
        }  

        UILabel* capitalLabel = (UILabel *) [cell.contentView viewWithTag:CapitalTag];
        UILabel*  stateLabel = (UILabel *) [cell.contentView viewWithTag:StateTag];

        capitalLabel.text=[news2 objectAtIndex:indexPath.row];
        stateLabel.text = [news3 objectAtIndex:indexPath.row];

       capitalLabel.backgroundColor = [UIColor clearColor];
        stateLabel.backgroundColor = [UIColor clearColor];

        capitalLabel.textColor=[UIColor whiteColor];
        stateLabel.textColor=[UIColor whiteColor];

        if(count<[news3 count]){
             capitalLabel.numberOfLines = 0;
            [capitalLabel sizeToFit];

            stateLabel.numberOfLines = 0;
            [stateLabel sizeToFit];

        }count++;

        return cell;
    }
 }

what changes should I do in my code ?

Upvotes: 0

Views: 979

Answers (4)

Sahil Mahajan
Sahil Mahajan

Reputation: 3990

Try to Set autoResizingMask so that view will automatically adjust itself during orientation change.

You can change the value in Xcode size inspector.

enter image description here

Upvotes: 0

Malek_Jundi
Malek_Jundi

Reputation: 6160

in iOS dont care about increase the width for the any control when change the orientation .. just set the autoresizing mask property for the 2 lables to be UIViewAutoresizingFlexibleWidth .. that means the width of your control will change according to the superview frame size.

Upvotes: 1

Abhinandan Sahgal
Abhinandan Sahgal

Reputation: 1076

You should override

 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
 // Return YES for supported orientations.
 return (interfaceOrientation == UIInterfaceOrientationPortrait);
 }

By checking the condition whether the current orientation is landscape or portrait you can set the frame of labels accordingly.

Upvotes: 1

Nekto
Nekto

Reputation: 17877

Try to set autoresizingMask to views that you want to automatically scale:

In if (cell == nil) after alloc+init of labels add:

capitalLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;

stateLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;

Upvotes: 1

Related Questions