2015-02-09 116 views
4

我有一個UICollectionView,它將具有帶有不同大小標籤的單元格。我正在嘗試根據標籤的大小來調整單元格大小。然而sizeForItemAtIndexPath我在那裏創建單元格似乎在cellForItemAtIndexPath之前被調用,我在其中設置了標籤..任何想法我可以在這裏做什麼?根據標籤大小調整UICollectionViewCell大小

- (void)getLabelSize:(UILabel *)label { 
    float widthIs = [label.text boundingRectWithSize:label.frame.size options:NSStringDrawingUsesLineFragmentOrigin attributes:@{ NSFontAttributeName:label.font } context:nil].size.width; 

    width = widthIs; 
} 

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { 
    // Configure the cell 
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath]; 
    UILabel *label = (UILabel *)[cell viewWithTag:1000]; 
    label.text = @"This is pretty long"; 
    [label sizeToFit]; 
    [self getLabelSize:label]; 
    NSLog([NSString stringWithFormat:@"%f", width]); 
    cell.backgroundColor = [UIColor whiteColor]; 

    return cell; 
} 

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath 
{ 
    return CGSizeMake(width, 50); 
} 

回答

0

首先,你必須存儲在一個臨時標籤您的留言(請確保它被隱藏或不可見)。然後獲取標籤寬度和高度如下:

templable.test = @"asdasdhasjdhajkdh"; 

templable.numberOfLines = 0; 

[templable sizeToFit]; 

float width = 0; 

width = self.templable.bounds.size.width+20; 
+0

這隻有在有一個標籤時纔有效。會有很多。 – 2015-02-09 12:20:01

+0

您必須爲這些添加陣列並在重新加載之前對其進行管理 – 2015-02-09 12:20:57

2

您需要存儲的標籤文字的地方,在陣列例如,和分別爲標籤定義字體和定義maxWidth,是這樣的:

#define kLabelFont [UIFont fontWithName:@"SourceSansPro-Regular" size:12.0f] 
#define maxWidth 100.0f 

然後修改方法getLabelSize,以字符串作爲參數:

- (CGSize)getLabelSize:(NSString *)string { 
    CGRect rect = [string boundingRectWithSize:(CGSize){maxWidth, CGFLOAT_MAX} options:NSStringDrawingUsesLineFragmentOrigin attributes:@{ NSFontAttributeName: kLabelFont } context:nil]; 
    return rect.size; 
} 

然後,你可以從兩種方法cellForItemAtIndexPath和sizeForItemAtIndexPath得到大小:

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { 
    ........ 
    NSString *string = [self.textArray objectAtIndex:indexPath.row]; 
    CGSize size = [self getLabelSize:string]; 
    ........ 
} 
+0

getLabelSize中仍然存在label.frame.size,但這不起作用。 – 2015-02-09 13:41:59

+0

對不起,我的錯誤,我修改了答案。 – 2015-02-09 13:52:59

7

迅速我已經根據它計算標籤的文字大小和更新單元尺寸:

func collectionView(_ collectionView: UICollectionView, 
         layout collectionViewLayout: UICollectionViewLayout, 
         sizeForItemAt indexPath: IndexPath) -> CGSize { 


     let size: CGSize = keywordArray[indexPath.row].size(attributes: [NSFontAttributeName: UIFont.systemFont(ofSize: 14.0)]) 
     return CGSize(width: size.width + 45.0, height: keywordsCollectionView.bounds.size.height) 
    } 

,因爲我有一個額外的按鈕,並填充我計算出額外的價值是45.0

+1

不要忘記添加'UICollectionViewDelegateFlowLayout'到你的類繼承中。 – 2017-07-06 01:34:53

+0

太棒了,對我有用!謝謝!) – 2018-01-24 16:55:34

+0

很高興幫助! @AntonEregin – Fay007 2018-02-12 08:59:35