Search code examples
objective-ciosxcodeuilabelsizetofit

UILabel Dynamic Content auto size trouble


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 !


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];
    }