请,我需要一些必须保存动态内容的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];
}
结果如下:
拜托,我需要建议!
谢谢!
答案 0 :(得分: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];
}
答案 1 :(得分: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];
}