0

拜托,我需要一些必须包含动态内容的 UILabel 的帮助。它们包含在 UIScrollView 中。

我无法让它们根据屏幕方向正确调整大小。我也想在他们之间保持准确的距离。现在,我遇到了其中 2 个问题(还有很多)。

我正在使用以下代码来调整它们的大小,但它没有得到预期的结果:

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

}

我在 shouldAutorotateToInterfaceOrientation: 中以相应的方向调用每个方法。

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

结果如下:

  • 视图加载的初始结果

视图加载的初始结果

  • 横向旋转后的结果

横向旋转后的结果

  • 转回纵向后的结果

转回纵向后的结果

拜托,我需要建议!

谢谢 !

4

2 回答 2

2

将所有标签从前到后存储在 NSArray 中。

现在制作一个根据方向设置标签框架的功能:

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

采用

if (UIInterfaceOrientationIsPortrait([UIDevice currentDevice].orientation))
{
  [self setFramesOfLabel:TRUE];
}
else
{    
  [self setFramesOfLabel:FALSE];
}
于 2012-09-19T04:51:06.403 回答
0

您似乎一遍又一遍地在相同的帧上重新操作。尝试使用此代码:

// 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
}

最后:

if (UIInterfaceOrientationIsPortrait([UIDevice currentDevice].orientation))
{
    [self updateLabelsPositionForPortrait];
}
else
{
    [self updateLabelsPositionForLandscape];
}
于 2012-09-19T00:53:04.193 回答