0

我正在尝试使用函数 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;
}
4

2 回答 2

3

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

于 2013-10-09T22:17:06.973 回答
0

我遇到了同样的问题。我有一个自定义单元格和一个单独的 tableview 控制器。事实证明,我们应该在控制器中设置 NumberOfLines 和 SizeToFit,而不是在单元格中。

于 2015-05-14T07:44:57.457 回答