gpt4 book ai didi

objective-c - UILabel 动态内容自动调整大小问题

转载 作者:行者123 更新时间:2023-11-28 22:48:49 24 4
gpt4 key购买 nike

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

结果如下:

  • View 加载的初始结果

Initial result on view load

  • 横向旋转后的结果

Result after rotation in Landscape

  • 旋转回纵向后的结果

Result after rotating back to portrait

拜托,我需要建议!

谢谢!

最佳答案

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

关于objective-c - UILabel 动态内容自动调整大小问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12487036/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com