3

在我的应用程序中是一个信息页。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];

}

请指教,提前谢谢

4

1 回答 1

4

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

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

}
于 2013-03-02T15:42:34.220 回答