2012-09-19 34 views
0

请帮我解决一些必须保存动态内容的UILabel。它们包含在UIScrollView中。UILabel动态内容自动大小问题

根据屏幕方向我无法让它们正确调整大小。我也想保持他们之间的确切距离。 现在,我遇到了其中两个(很多来)的麻烦。

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

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

结果是这些:

  • 上视图负载初始结果在景观
旋转之后

Initial result on view load

  • 结果旋转回纵向

Result after rotating back to portrait

Result after rotation in Landscape

  • 结果拜托,我需要咨询!

    谢谢!

回答

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

非常优雅的解决方案:)!我会在几分钟内尝试一下! – Razvan

+0

谢谢!它适用于一些变化。我会编辑你的代码。 – Razvan

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

它不工作....你不能比较的CGRect(这是一个结构)但我用CGRectIsEmpty代替 – Razvan

+0

是的,关于'CGRectIsEmpty',我写了代码。但是,它对4条线路中的任何一条线路都不起作用,还是只是其中之一? – Ravi