2013-10-09 23 views
0

我想使用函数sizeToFit。它在整个应用程序中都能正常工作,但它在下面的代码中不起作用。除此之外,如果不能像我这样做,请告知。为什么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; 
} 

回答

3

由于标签是空的,sizeToFit收缩下来落空。这才是重点。设置一些文字,然后拨打sizeToFit,标签将足够大,可供文本使用。

+0

该信息来自self.description –

+1

所调用的数组。这并不重要。您需要在设置标签的text属性后调用'sizeToFit',而不是之前。在你发布的代码中,你在调用'sizeToFit'之前没有设置'text'属性。 – rmaddy

+0

好的,你能告诉我如何实现这个目标吗? –

0

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