Question

Please, I need some help with some UILabels that must hold dynamic content. They are contained in a UIScrollView.

I can't get them to resize properly depending on screen orientation. I also want to keep an exact distance between them. Right now, I'm having trouble with 2 of them (many to come).

I'm using the following code to resize them but it doesn't get me the expected results:

    - (void) updateLabelsPositionForPortrait
{
    summary_PersonalMonth.numberOfLines = 0;
    summary_PersonalDay.numberOfLines = 0;

    summary_PersonalMonth.frame = CGRectMake(summary_PersonalMonth.frame.origin.x,
                                             summary_PersonalMonth.frame.origin.y,
                                             summary_PersonalMonth.frame.size.width + 15,
                                             summary_PersonalMonth.frame.size.height);
    [summary_PersonalMonth sizeToFit];

    summary_PersonalDay.frame = CGRectMake(summary_PersonalDay.frame.origin.x,
                                           summary_PersonalDay.frame.origin.y + summary_PersonalMonth.bounds.origin.y + 10,
                                           summary_PersonalDay.frame.size.width + 15,
                                           summary_PersonalDay.frame.size.height);
    [summary_PersonalDay sizeToFit];

    [self updateScrollViewContentSize];
}

- (void) updateLabelsPositionForLandscape
{
    summary_PersonalMonth.numberOfLines = 1;
    summary_PersonalDay.numberOfLines = 1;

    summary_PersonalMonth.frame = CGRectMake(summary_PersonalMonth.frame.origin.x,
                                             summary_PersonalMonth.frame.origin.y,
                                             summary_PersonalMonth.frame.size.width,
                                             summary_PersonalMonth.frame.size.height);
    [summary_PersonalMonth sizeToFit];

    summary_PersonalDay.frame = CGRectMake(summary_PersonalDay.frame.origin.x,
                                           summary_PersonalDay.frame.origin.y - summary_PersonalMonth.bounds.origin.y - 10,
                                           summary_PersonalDay.frame.size.width,
                                           summary_PersonalDay.frame.size.height);
    [summary_PersonalDay sizeToFit];

}

and I call each method in shouldAutorotateToInterfaceOrientation: in the corresponding orientation.

if (UIInterfaceOrientationIsPortrait([UIDevice currentDevice].orientation))
{
    [self updateLabelsPositionForPortrait];
}
else
{
    [self updateLabelsPositionForLandscape];
}

The results are these:

  • Initial result on view load

Initial result on view load

  • Result after rotation in Landscape

Result after rotation in Landscape

  • Result after rotating back to portrait

Result after rotating back to portrait

Please, I need advice !

Thank you !

Was it helpful?

Solution

Store all labels starting form first to last in position in NSArray.

Now making one function which set frames of labels according to orientation:

-(void)setFramesOfLabel:(BOOL)flag
{
    if(flag) //portrait
    {
      int height = 20;
      int spaceBetweenLabels = 20;
      itn xspace = 20 //
    }
    else//landscape changes
    {
      int height = 20;
      int spaceBetweenLabels = 20;
      itn xspace = 20 /
    }
    int count = 0;
    for(UILabel *label in arrLabels)
    {
       NSString *strText = [arrTexts objectAtIndex:count]; //as u may having text array to fill in labels as i assume
       CGSize expectedLabelSize = [strText sizeWithFont:label.font constrainedToSize:maximumLabelSize lineBreakMode:UILineBreakModeWordWrap]; //

       CGrect *newFrame = CGRectMake(xspace,height,expectedLabelSize.width,expectedLabelSize.height);
       [label setFrame:newFrame];

        height += expectedLabelSize.height;
       count ++;
    }
    [scrollView setContentSize(scrollView.width,height)];
}

Use

if (UIInterfaceOrientationIsPortrait([UIDevice currentDevice].orientation))
{
  [self setFramesOfLabel:TRUE];
}
else
{    
  [self setFramesOfLabel:FALSE];
}

OTHER TIPS

You seem to be re-operating on the same frames over and over again. Try using this code:

// Declare the below variables as globals
CGRect defaultRectPortrait = CGRectZero;
CGRect defaultRectLandscape = CGRectZero;

- (void) updateLabelsPositionForPortrait
{
    summary_PersonalMonth.numberOfLines = 0;
    summary_PersonalDay.numberOfLines = 0;

    if(defaultRectPortait == CGRectZero)
        defaultRectPortait = CGRectMake(summary_PersonalMonth.frame.origin.x,
                                             summary_PersonalMonth.frame.origin.y,
                                             summary_PersonalMonth.frame.size.width + 15,
                                             summary_PersonalMonth.frame.size.height);

    [summary_PersonalMonth setFrame:defaultRectPortrait];
    [summary_PersonalMonth sizeToFit];

    // Do a similar thing for summary_PersonalDay

    [self updateScrollViewContentSize];
}    

- (void) updateLabelsPositionForLandscape
{
    // Use similar logic for summary_PersonalMonth and summary_PersonalDay in landscape as well
}

And finally:

if (UIInterfaceOrientationIsPortrait([UIDevice currentDevice].orientation))
{
    [self updateLabelsPositionForPortrait];
}
else
{
    [self updateLabelsPositionForLandscape];
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top