0

私はかなり長い間検索してきましたが、見つけたすべての答えがうまくいかないようです。別のビューの YouTube ビデオの説明から取得した一連のテキストを保持するビュー (以下のコード) があります。明らかに、ビデオの説明の長さが異なります。これが問題を引き起こします。ラベルの最後のテキスト行の後、スクロールビューが約 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:@"\n\nFor the best viewing experience, please use WiFi."];
    VideoDescription = [VideoDescription stringByAppendingString:@"\n\nWant an app like this for your YouTube Channel? Go to www.apps4tubers.com to find out how you can get one!"];
    VideoDescription = [VideoDescription stringByAppendingString:@"\n\nDon'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:@"\n\nFor the best viewing experience, please use WiFi."];
    VideoDescription = [VideoDescription stringByAppendingString:@"\n\nWant 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:@"\n\nWant 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:@"\n\nFor 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];
4

2 に答える 2

0

つまり、NSStringメソッド-sizeWithFont:forWidth:lineBreakMode:を使用して、指定された幅と改行モードで文字列がコンテナーにレンダリングされる高さを決定できます。それを使用してUILabel、スクロールビューのコンテンツサイズを適切にサイズ設定したり、設定したりできます。

于 2013-03-07T02:44:18.450 に答える