我正在尝试使用函数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];
我使用此方法从不同的控制器传递数据
- (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;
}
答案 0 :(得分:3)
由于标签为空,sizeToFit
将其缩小为空。这才是重点。设置一些文本,然后调用sizeToFit
,标签将足够大的文本。
答案 1 :(得分:0)
我遇到了同样的问题。我有一个自定义单元格和一个独立的tableview控制器。事实证明我们应该在控制器中设置NumberOfLines和SizeToFit,而不是单元格。