gpt4 book ai didi

ios - 为什么 sizeToFit 会删除我的整个标签?

转载 作者:行者123 更新时间:2023-11-28 20:13:13 26 4
gpt4 key购买 nike

我正在尝试使用函数 sizeToFit。它在整个应用程序中完美运行,但在下面的代码中不起作用。此外,如果无法按照我的方式完成,请提出建议。

            self.label = [[UILabel alloc] initWithFrame:CGRectMake(10.0, 67.0, 290, 50)];
self.label.font= [UIFont fontWithName:@"Helvetica-Bold" size:20];
self.label.textColor = [UIColor colorWithRed:33.0f/255.0f green:74.0f/255.0f blue:146.0f/255.0f alpha:1.0f];
self.label.numberOfLines = 0;
self.label.lineBreakMode = NSLineBreakByWordWrapping;
self.label.backgroundColor = [UIColor whiteColor];
[self.contentView addSubview:self.label];

self.description = [[UILabel alloc] initWithFrame:CGRectMake(10.0, 380.0, 300, 400)];
self.description.font= [UIFont fontWithName:@"Helvetica-Light" size:15];
self.description.textColor = [UIColor blackColor];
self.description.textAlignment = NSTextAlignmentJustified;
self.description.numberOfLines = 0;
self.description.lineBreakMode = NSLineBreakByWordWrapping;
self.description.backgroundColor = [UIColor whiteColor];
[self.description sizeToFit];
[self.contentView addSubview:self.description];


self.image = [[UIImageView alloc] initWithFrame:CGRectMake(20, 130, 280, 228)];
self.image.clipsToBounds = YES;
//[scrollView addSubview:self.image];
[self.contentView addSubview:self.image];



self.backgroundColor = [UIColor whiteColor];

我使用这个方法从不同的 Controller 传递数据

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
Cell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];
NSDictionary *item = [_articleListmain objectAtIndex:indexPath.item];
// set the article image
[cell.image setImageWithURL:[item objectForKey:@"image"]];
[cell.label setText:[item objectForKey:@"title"]];
[cell.description setText:[item objectForKey:@"description"]];



return cell;
}

最佳答案

由于标签是空的,sizeToFit 将其缩小为空。这才是重点。设置一些文本,然后调用 sizeToFit,标签将刚好足够容纳文本。

关于ios - 为什么 sizeToFit 会删除我的整个标签?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19283630/

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