【问题标题】:UILabel Dynamic Content auto size troubleUILabel 动态内容自动调整大小问题
【发布时间】:2012-09-19 00:39:07
【问题描述】:

拜托,我需要一些关于必须包含动态内容的 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];
}

结果如下:

  • 视图加载的初始结果

  • 横向旋转后的结果

  • 转回纵向后的结果

拜托,我需要建议!

谢谢!

【问题讨论】:

    标签: objective-c ios xcode uilabel sizetofit


    【解决方案1】:

    将所有标签从前到后存储在 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];
    }
    

    【讨论】:

    • 非常优雅的解决方案 :) !我会在几分钟内尝试一下!
    【解决方案2】:

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

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

    【讨论】:

    • 它不工作....你不能比较 CGRect (它是一个结构),但我使用 CGRectIsEmpty 代替
    • 是的,关于CGRectIsEmpty,我是即时编写代码的。但它对 4 行中的任何一行都不起作用还是只是其中之一?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-16
    相关资源
    最近更新 更多