自动调整UIScrollView大小以适合UILabel文本



我一直在寻找相当一段时间了,我找到的每一个答案似乎都不起作用。我有一个视图,下面的代码,它保存了一堆文本,这些文本是从另一个视图的YouTube视频描述中提取的。很明显,每个视频的描述长度不同,这就是问题所在。我想使scrollview只向下滚动到标签的最后一个文本行之后大约10像素。

在这段代码中,我没有这样做,我有一个固定的大小

有谁能帮忙吗?我已经找了好几天了,我似乎找到了一个合适的解决办法。

self.view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
VideoDescription = [VideoDescription stringByTrimmingCharactersInSet:
                    [NSCharacterSet whitespaceAndNewlineCharacterSet]];
//int a = VideoDescription.length;
//NSLog([NSString stringWithFormat:@"%i", a]);
if(VideoDescription.length < 310)
{
    VideoDescription = [VideoDescription stringByAppendingString:@"n_______________________________________________"];
    VideoDescription = [VideoDescription stringByAppendingString:@"nnFor the best viewing experience, please use WiFi."];
    VideoDescription = [VideoDescription stringByAppendingString:@"nnWant an app like this for your YouTube Channel? Go to www.apps4tubers.com to find out how you can get one!"];
    VideoDescription = [VideoDescription stringByAppendingString:@"nnDon't forget to rate this app! Love it? Hate it? Regardless we love to hear your comments and suggestions!"];
}
else if(VideoDescription.length < 380)
{
    VideoDescription = [VideoDescription stringByAppendingString:@"n_______________________________________________"];
    VideoDescription = [VideoDescription stringByAppendingString:@"nnFor the best viewing experience, please use WiFi."];
    VideoDescription = [VideoDescription stringByAppendingString:@"nnWant an app like this for your YouTube Channel? Go to www.apps4tubers.com to find out how you can get one!"];
}
else if(VideoDescription.length > 379 && VideoDescription.length < 410)
{
    VideoDescription = [VideoDescription stringByAppendingString:@"n_______________________________________________"];
    VideoDescription = [VideoDescription stringByAppendingString:@"nnWant an app like this for your YouTube Channel? Go to www.apps4tubers.com to find out how you can get one!"];
}
else
{
    VideoDescription = [VideoDescription stringByAppendingString:@"nnFor the best viewing experience, please use WiFi."];
}
CGRect scrollViewFrame = CGRectMake(0, 0, 320, 480);
TheView = [[UIScrollView alloc] initWithFrame:scrollViewFrame];
CGSize scrollViewContentSize = CGSizeMake(320, 650);
[TheView setContentSize:scrollViewContentSize];
[TheView setPagingEnabled:NO];
TheView.showsHorizontalScrollIndicator = NO;
TheView.showsVerticalScrollIndicator = YES;
TheView.bounces = NO;
[self.view addSubview:TheView];
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:@"Back" style:UIBarButtonItemStylePlain target:nil action:nil];
self.navigationItem.backBarButtonItem = backButton;
Background = [[UILabel alloc] initWithFrame: CGRectMake(00, 25, 1000, 1600)];
Background.center = CGPointMake(00, 00);
Background.backgroundColor = [self colorWithHexString:@"111625"];
[TheView addSubview: Background];
VideoDecrip = [[UILabel alloc] initWithFrame: CGRectMake(00, 25, 320, 400)];
VideoDecrip.center = CGPointMake(160.5, 400);
VideoDecrip.text = VideoDescription;
VideoDecrip.font = [UIFont fontWithName: @"Cochin-Bold" size: 13.5];
VideoDecrip.textColor = [self colorWithHexString:@"D1E751"];
VideoDecrip.shadowColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:.35];
VideoDecrip.layer.shadowOpacity = 0;
VideoDecrip.shadowOffset = CGSizeMake(1,1);
VideoDecrip.layer.masksToBounds = NO;
VideoDecrip.backgroundColor = [UIColor clearColor];
VideoDecrip.textAlignment = NSLineBreakByWordWrapping;
VideoDecrip.adjustsFontSizeToFitWidth = YES;
VideoDecrip.numberOfLines = 0;
[VideoDecrip sizeToFit];
[TheView addSubview: VideoDecrip];
NSLog(@"%i", VideoDecrip.text.length);
VideoTitleBorder = [[UILabel alloc] initWithFrame: CGRectMake(00, 25, 320.5, 30)];
VideoTitleBorder.center = CGPointMake(160, 182.5);
VideoTitleBorder.shadowOffset = CGSizeMake(1,1);
VideoTitleBorder.backgroundColor = [self colorWithHexString:@"0B486B"];
VideoTitleBorder.layer.borderColor = [UIColor darkGrayColor].CGColor;
VideoTitleBorder.layer.borderWidth = .4;
[TheView addSubview: VideoTitleBorder];
VideoTitle = [[UILabel alloc] initWithFrame: CGRectMake(00, 25, 320, 26)];
VideoTitle.text = VidTitle;
if([VideoTitle.text length] < 20)
{
    VideoTitle.center = CGPointMake(160, 182.5);
}
else if([VideoTitle.text length] < 30)
{
    VideoTitle.center = CGPointMake(160, 182);
}
else
{
    VideoTitle.center = CGPointMake(160, 179);
}
VideoTitle.font = [UIFont fontWithName: @"ArialRoundedMTBold" size: 25];
VideoTitle.textColor = [self colorWithHexString:@"BEF202"];
VideoTitle.shadowColor = [UIColor blackColor];
VideoTitle.backgroundColor = [UIColor clearColor];
VideoTitle.shadowOffset = CGSizeMake(1,1);
VideoTitle.textAlignment = NSTextAlignmentCenter;
VideoTitle.adjustsFontSizeToFitWidth = YES;
[TheView addSubview: VideoTitle];
VideoTitleBorder2 = [[UILabel alloc] initWithFrame: CGRectMake(00, 25, 160, 500)];
VideoTitleBorder2.center = CGPointMake(400, 125);
VideoTitleBorder2.shadowOffset = CGSizeMake(1,1);
VideoTitleBorder2.backgroundColor = [self colorWithHexString:@"0B486B"];
VideoTitleBorder2.layer.borderColor = [UIColor darkGrayColor].CGColor;
VideoTitleBorder2.layer.borderWidth = 2.0;
[self.view addSubview: VideoTitleBorder2];

简而言之,您可以使用NSString方法-sizeWithFont:forWidth:lineBreakMode:来确定字符串在给定宽度和断行模式下在容器中呈现的高度。你可以用它来调整UILabel的大小,并适当地设置scrollview的内容大小。

尝试在标签上使用sizeToFit,然后设置scrollView的contentSize:

[label sizeToFit];  
[scrollView setContentSize:CGSizeMake(CGRectGetWidth(self.view.frame), CGRectGetHeight(label.frame) + 10)];

使用sizeToFit是非常好的,只是要确保你的标签在调用它之前已经是正确的宽度。

最新更新