在我的应用中是一个信息页。文本长度可能会因本地化而有所不同。此外,它的屏幕不动产比4英寸显示器少3.5英寸。因此,我试图实现的是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/