gpt4 book ai didi

ios - 如何自动调整 UIScrollView 的大小

转载 作者:塔克拉玛干 更新时间:2023-11-02 07:48:53 25 4
gpt4 key购买 nike

在我的应用程序中有一个信息页面。由于本地化,textllength 可能会有所不同。除此之外,3.5 英寸的屏幕比 4 英寸的显示器占用更少的空间。所以我试图实现的是 UILabel 在 ScrollView 内“增长”并且 Scrollview 适应屏幕。

这是我的代码:

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.

// Create a ScrollView and a label
UIScrollView *infoScroll = [[UIScrollView alloc] initWithFrame:CGRectMake(20.0f, 55.0f, 300.0f, 400.0f)];

CGRect labelFrame = CGRectMake(0, 0, 280, 800);
UILabel *infoLabel = [[UILabel alloc] initWithFrame:labelFrame];

NSString *labelText = NSLocalizedString (@"InfoText",@"");
[infoLabel setText:labelText];

// Tell the label to use an unlimited number of lines
[infoLabel setNumberOfLines:0];
[infoLabel setBackgroundColor:[UIColor clearColor]];
[infoLabel setFont:[UIFont boldSystemFontOfSize:17]];
infoLabel.textColor = [UIColor colorWithRed:255 green:255 blue:255 alpha:1.0];
[infoLabel sizeToFit];
infoScroll.contentSize = CGSizeMake(infoScroll.contentSize.width, infoLabel.frame.size.height);
[infoScroll addSubview:infoLabel];

[self.view addSubview:infoScroll];

}

请指教,提前致谢

最佳答案

通过以下方式修改您的功能:

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.

// Create a ScrollView and a label
UIScrollView *infoScroll = [[UIScrollView alloc] initWithFrame:CGRectMake(20.0f, 55.0f, self.view.frame.size.width - 20.0, self.view.frame.size.height - 55)];
infoScroll.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;

UILabel *infoLabel = [[UILabel alloc] initWithFrame:CGRectZero];
infoLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;

NSString *labelText = NSLocalizedString (@"InfoText",@"");
[infoLabel setText:labelText];

// Tell the label to use an unlimited number of lines
[infoLabel setNumberOfLines:0];
[infoLabel setBackgroundColor:[UIColor clearColor]];
[infoLabel setFont:[UIFont boldSystemFontOfSize:17]];
infoLabel.textColor = [UIColor colorWithRed:255 green:255 blue:255 alpha:1.0];
//[infoLabel sizeToFit];

CGSize infoLabelSize = [infoLabel.text sizeWithFont:infoLabel.font
constrainedToSize:CGSizeMake(infoScroll.frame.size.width, 5000)
lineBreakMode:UILineBreakModeWordWrap];

infoLabel.frame = CGRectMake(0, 0, infoLabelSize.width, infoLabelSize.height);
infoScroll.contentSize = infoLabelSize;
[infoScroll addSubview:infoLabel];

[self.view addSubview:infoScroll];

}

关于ios - 如何自动调整 UIScrollView 的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15176089/

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