我正在尝试使用功能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;
}

最佳答案

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

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

10-13 08:56