Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/104.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c UILabel动态内容自动大小问题_Objective C_Ios_Xcode_Uilabel_Sizetofit - Fatal编程技术网

Objective c UILabel动态内容自动大小问题

Objective c UILabel动态内容自动大小问题,objective-c,ios,xcode,uilabel,sizetofit,Objective C,Ios,Xcode,Uilabel,Sizetofit,请,我需要一些UILabel的帮助,这些UILabel必须包含动态内容。它们包含在UIScrollView中 我无法根据屏幕方向正确调整它们的大小。我还想在他们之间保持精确的距离。 现在,我和其中的两个(很多都会来)有麻烦 我正在使用以下代码调整它们的大小,但没有得到预期的结果: - (void) updateLabelsPositionForPortrait { summary_PersonalMonth.numberOfLines = 0; summary_Perso

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

我无法根据屏幕方向正确调整它们的大小。我还想在他们之间保持精确的距离。 现在,我和其中的两个(很多都会来)有麻烦

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

    - (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];
}
结果如下:

  • 视图加载时的初始结果

  • 在景观中旋转后的结果

  • 旋转回纵向后的结果

求你了,我需要建议


谢谢大家!

您似乎一次又一次地在相同的帧上操作。尝试使用以下代码:

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

在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)];
}
使用


它不起作用了。。。。你不能比较CGRect(它是一个结构),但我用CGRectIsEmpty代替了。是的,关于
CGRectIsEmpty
,我动态地写了代码。但它对这4条线路中的任何一条都不起作用,还是仅仅是其中的一条?非常优雅的解决方案:)!我几分钟后就试试看!
if (UIInterfaceOrientationIsPortrait([UIDevice currentDevice].orientation))
{
  [self setFramesOfLabel:TRUE];
}
else
{    
  [self setFramesOfLabel:FALSE];
}