小编Vin*_*eet的帖子

如何使用SWIFT以编程方式设置UICollectionViewCell大小取决于其中UIlabel的动态大小?

我有一个任务,我要准备一个简单的GridView,几乎没有约束.我想过使用UICollectionView并且还对网做了一些研究.我在这里发现了一篇博文,其内容与我希望实现的类似.

现在,它使用customCollectionViewCell行和列的类.每个细胞都有一个UILabel.UILabel因此,这可以具有动态文本,单元格应根据动态调整其大小UILabel.

请找到以下所有代码 -

UICollectionView 用于行单元格

class RowCell: UICollectionViewCell
{
    var textLabel : UILabel!

    override init(frame: CGRect)
    {
        super.init(frame: frame)

        textLabel = UILabel(frame: CGRectMake(0, 0, 65, 35))
        textLabel.font = UIFont.systemFontOfSize(UIFont.smallSystemFontSize())
        textLabel.textAlignment = .Center
        textLabel.numberOfLines = 0
        textLabel.backgroundColor = UIColor.clearColor()
        textLabel.lineBreakMode = NSLineBreakMode.ByCharWrapping
        textLabel.sizeThatFits(CGSizeMake(self.bounds.width, self.bounds.height))

        contentView.addSubview(textLabel)
        self.layer.borderWidth = 0.7
        self.layer.borderColor = charcoalColor.CGColor
        self.frame = CGRectMake(frame.origin.x, frame.origin.y, textLabel.frame.width*8, textLabel.frame.height*8)

    }

    required init(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented") …
Run Code Online (Sandbox Code Playgroud)

ios uicollectionview uicollectionviewcell uicollectionviewlayout swift

5
推荐指数
1
解决办法
1185
查看次数