UICollectionView的动态单元格宽度取决于标签宽度

时间:2014-04-17 13:49:46

标签: ios objective-c swift uicollectionview uicollectionviewcell

我有一个UICollectionView,它从可重复使用的单元格中加载单元格,其中包含标签。数组为该标签提供内容。我可以使用sizeToFit轻松调整标签宽度,具体取决于内容宽度。但我不能让细胞符合标签。

这是代码

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    arrayOfStats =  @[@"time:",@"2",@"items:",@"10",@"difficulty:",@"hard",@"category:",@"main"];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:     (NSInteger)section{
    return [arrayOfStats count];
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{

    return CGSizeMake(??????????);
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{

    return 1;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{

    Cell *cell = (Cell *) [collectionView dequeueReusableCellWithReuseIdentifier:@"qw" forIndexPath:indexPath];
    cell.myLabel.text = [NSString stringWithFormat:@"%@",[arrayOfStats objectAtIndex:indexPath.item]];
    // make label width depend on text width
    [cell.myLabel sizeToFit];

    //get the width and height of the label (CGSize contains two parameters: width and height)
    CGSize labelSize = cell.myLbale.frame.size;

    NSLog(@"\n width  = %f height = %f", labelSize.width,labelSize.height);

    return cell;
}

8 个答案:

答案 0 :(得分:69)

sizeForItemAtIndexPath中返回文字的大小

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{

    return [(NSString*)[arrayOfStats objectAtIndex:indexPath.row] sizeWithAttributes:NULL];
}

答案 1 :(得分:26)

检查下面的代码,您可能会给出非常短的CGSize。

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{

    NSString *testString = @"SOME TEXT";
    return [testString sizeWithAttributes:NULL];
}

答案 2 :(得分:18)

在Swift 3中

let size = (arrayOfStats[indexPath.row] as NSString).size(attributes: nil)

答案 3 :(得分:15)

Swift 4.2 +

原理是:

  1. 实现UICollectionViewDelegateFlowLayout(包含必要的方法签名)

  2. 调用collectionView...sizeForItemAt方法。

  3. 无需将String桥接到NSString来调用size(withAttributes:方法。 Swift String可以立即使用。

  4. 属性与您为(NS)AttributedString设置的属性相同,即字体系列,大小,粗细等。可选参数。


示例解决方案:

extension ViewController: UICollectionViewDelegateFlowLayout {
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        return "String".size(withAttributes: nil)
    }
}

但是您很可能希望指定与您的单元格相对应的具体字符串属性,因此最终返回将类似于:

// Replace "String" with proper string (typically array element). 
return "String".size(withAttributes: [
    NSAttributedString.Key.font : UIFont.boldSystemFont(ofSize: 14)
])

答案 4 :(得分:11)

我发现了一个快速4.2的小技巧

对于动态宽度和固定高度:

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        let label = UILabel(frame: CGRect.zero)
        label.text = textArray[indexPath.item]
        label.sizeToFit()
        return CGSize(width: label.frame.width, height: 32)
    }

对于动态高度和固定宽度:

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
            let label = UILabel(frame: CGRect.zero)
            label.text = textArray[indexPath.item]
            label.sizeToFit()
            return CGSize(width: 120, height: label.frame.height)
        }

答案 5 :(得分:9)

快捷键4

let size = (arrayOfStats[indexPath.row] as NSString).size(withAttributes: nil)

答案 6 :(得分:-1)

//添加视图didload

struct IPAddress
{
    IPAddress(const unsigned char values[4]) :
    values{values} {} // <-- copy as a whole

    values_t<const unsigned char, 4> values;
};

答案 7 :(得分:-1)

我的 UICollectionViewFlowLayout() 的这一行对我有用:

collectionViewLayout.estimatedItemSize = UICollectionViewFlowLayout.automaticSize