[英]Dynamic cell width of UICollectionView depending on label width
I have a UICollectionView, that loads cells from reusable cell, which contains label.我有一个 UICollectionView,它从包含 label 的可重复使用的单元格加载单元格。 An array provides content for that label.
一个数组为该 label 提供内容。 I can resize label width depending on content width easily with sizeToFit.
我可以使用 sizeToFit 根据内容宽度轻松调整 label 宽度。 But I cannot make cell to fit label.
但我无法制作适合 label 的电池。
Here's the code这是代码
- (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;
}
In sizeForItemAtIndexPath
return the size of the text 在
sizeForItemAtIndexPath
返回文本的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
return [(NSString*)[arrayOfStats objectAtIndex:indexPath.row] sizeWithAttributes:NULL];
}
Checkout below code you might be giving very short CGSize. 结帐以下代码,您可能会给出非常短的CGSize。
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
NSString *testString = @"SOME TEXT";
return [testString sizeWithAttributes:NULL];
}
Swift 4.2+ Swift 4.2+
Principle is: 原则是:
Implement UICollectionViewDelegateFlowLayout
(it contains necessary method signature) 实现
UICollectionViewDelegateFlowLayout
(它包含必要的方法签名)
Call collectionView...sizeForItemAt
method. 调用
collectionView...sizeForItemAt
方法。
No need to bridge-cast String
to NSString
to call size(withAttributes:
method. Swift String
has it out of the box. 不需要将
String
为NSString
以调用size(withAttributes:
方法size(withAttributes:
String
开箱即用。
Attributes are the same you set for (NS)AttributedString
, ie font family, size, weight, etc. Optional parameter. 属性与您为
(NS)AttributedString
设置的相同,即字体系列,大小,重量等。可选参数。
Sample solution: 样品溶液:
extension ViewController: UICollectionViewDelegateFlowLayout {
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return "String".size(withAttributes: nil)
}
}
But you would most likely want to specify concrete string attributes respective to your cell, hence final return would look like: 但是您很可能希望指定与您的单元格相对应的具体字符串属性,因此最终返回将如下所示:
// Replace "String" with proper string (typically array element).
return "String".size(withAttributes: [
NSAttributedString.Key.font : UIFont.boldSystemFont(ofSize: 14)
])
I have found a small trick for swift 4.2 我找到了一个关于swift 4.2的小技巧
For dynamic width & fixed height: 对于动态宽度和固定高度:
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)
}
For dynamic height & fixed width: 对于动态高度和固定宽度:
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)
}
在Swift 3中
let size = (arrayOfStats[indexPath.row] as NSString).size(attributes: nil)
斯威夫特4
let size = (arrayOfStats[indexPath.row] as NSString).size(withAttributes: nil)
In Swift 5.X, this single line of code is enough.在 Swift 5.X 中,这一行代码就足够了。
In the below answer i have added dynamic width and static height.在下面的答案中,我添加了动态宽度和静态高度。 Based on your requirement change height value.
根据您的要求更改高度值。
//MARK: - UICollectionViewDelegateFlowLayout
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: itemsArray[indexPath.item].size(withAttributes: [NSAttributedString.Key.font : UIFont.systemFont(ofSize: 17)]).width + 25, height: 30)
}
Add UICollectionViewDelegateFlowLayout to your delegate将UICollectionViewDelegateFlowLayout添加到您的委托
It's perfectly worked to me with equal cell space with dynamic cell size based on text length .它对我来说非常有用,具有基于文本长度的动态单元格大小的相等单元格空间。
PFA image below...下面的 PFA 图像...
//Create UICollectionView lazy var collectionView: UICollectionView = { //创建UICollectionView惰性变量collectionView: UICollectionView = {
let layout = UICollectionViewFlowLayout()
layout.scrollDirection = .horizontal
//CollectionCellView width autoSize
layout.estimatedItemSize = UICollectionViewFlowLayout.automaticSize
let collectionView = UICollectionView(frame: .zero, collectionViewLayout: layout)
collectionView.translatesAutoresizingMaskIntoConstraints = false
collectionView.register(CustomCollectionViewCell.self, forCellWithReuseIdentifier: cellId)
[enter image description here][1]
return collectionView
}()
//add in view didload //添加视图didload
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
[layout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
layout.estimatedItemSize = CGSizeMake(self.breadScrumbCollectionView.frame.size.width, 30);
self.breadScrumbCollectionView.collectionViewLayout = layout;
我UICollectionViewFlowLayout()
这一行对我UICollectionViewFlowLayout()
:
collectionViewLayout.estimatedItemSize = UICollectionViewFlowLayout.automaticSize
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.