菜鸟教程小白 发表于 2022-12-11 18:29:58

ios - Swift 给 uicollectionviewcell 宽度和动态高度


                                            <p><p>我有这个 uicollectionviewcell</p>

<pre><code>func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -&gt; UICollectionViewCell {
    let cell = commentSection.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath) as! CommentCell
    return cell
}
</code></pre>

<p><code>CommentCell</code>:</p>

<pre><code>let CommenterprofileImage: UIImageView = {
      let v = UIImageView()
      v.translatesAutoresizingMaskIntoConstraints = false
      return v
    }()
    let commentText: UILabel = {
      let l = UILabel()
      l.numberOfLines = 0
      l.text = &#34;some text&#34;
      l.translatesAutoresizingMaskIntoConstraints = false
      return l
    }()
    override init(frame: CGRect) {
      super.init(frame: frame)
      backgroundColor = .blue
      designCell()
    }
    func designCell(){

      addSubview(CommenterprofileImage)
      addSubview(commentText)

      addCellConstraints()
    }

    func addCellConstraints(){
      CommenterprofileImage.topAnchor.constraint(equalTo: self.topAnchor,constant:10).isActive = true
      CommenterprofileImage.leftAnchor.constraint(equalTo: self.leftAnchor,constant:20).isActive = true
      CommenterprofileImage.widthAnchor.constraint(equalToConstant: 70).isActive = true
      CommenterprofileImage.heightAnchor.constraint(equalToConstant: 70).isActive = true

      commentText.topAnchor.constraint(equalTo: CommenterprofileImage.bottomAnchor,constant:20).isActive = true
      commentText.leftAnchor.constraint(equalTo: self.leftAnchor).isActive = true
      commentText.rightAnchor.constraint(equalTo: self.rightAnchor).isActive = true
    }
</code></pre>

<p>我希望这个单元格的宽度等于 <code>view</code> 的宽度,但我希望它的高度取决于它的内容。
我试过这样做</p>

<pre><code>layout.estimatedItemSize = CGSize(width: view.frame.width, height: 100)
</code></pre>

<p>但是我的单元格的高度是 100,宽度小于 100。</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>你可以这样做-:</p>

<p>先用<code>boundingRect</code>方法计算标签高度-:</p>

<pre><code>func estimatedFrameHeight(text:String) -&gt; CGRect{
      let size = CGSize(width: (view.frame.width/2 - 8), height: 1000)
      let options = NSStringDrawingOptions.usesFontLeading.union(.usesLineFragmentOrigin)
      let attribute =
      return NSString(string: text).boundingRect(with: size, options: options, attributes: attribute, context: nil)
    }
</code></pre>

<p>保留您提供给标签的完全相同的字体,并保留 <code>.usesLineFragmentOrigin</code> 用于多行标签。</p>

<p>现在在 CollectionView <code>sizeForItemAt indexPath</code> 中执行此操作-:</p>

<pre><code>func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -&gt; CGSize {
      let width = (collectionView.frame.width)
      let subDict = subCategoryListArray
      if let product_name = subDict[&#34;product_name&#34;] as? String {
      let height = estimatedFrameHeight(text:product_name)
      return CGSize(width: width, height: height.height + 70 + 10 + 20)
    }
      return CGSize(width: 0, height: 0)

    }
</code></pre>

<p><code>让高度 =estimatedFrameHeight(text:product_name)</code> 。如果需要,这将为您提供估计的高度和宽度。现在您已经计算出标签高度,您需要添加 imageView 高度、y 位置约束以使其工作。您可以从这里获取它-:</p>

<pre><code>CommenterprofileImage.heightAnchor.constraint(equalToConstant: 70).isActive = true

Height = 70 you have.
</code></pre>

<p>还有,</p>

<pre><code>CommenterprofileImage.topAnchor.constraint(equalTo: self.topAnchor,constant:10).isActive = true

commentText.topAnchor.constraint(equalTo: CommenterprofileImage.bottomAnchor,constant:20).isActive
</code></pre>

<p>顶部约束 = <code>10</code></p>

<p>顶部约束 = <code>20</code></p>

<p>所以你现在需要添加这个,正如你在回答中看到的那样。</p>

<p><strong>备注-:</strong></p>

<blockquote>
<p>only add ImageView Height if it is above label else not require,just
add height and y padding for what ever you have above label.</p>

<p>Same to calculate exact width for label subtarct leading or trailing
space.</p>
</blockquote>

<p><strong>如果标签覆盖整个宽度(不需要插入减法)-:</strong></p>

<pre><code>func estimatedFrameHeight(text:String) -&gt; CGRect{
      let size = CGSize(width: (view.frame.width), height: 1000)
      let options = NSStringDrawingOptions.usesFontLeading.union(.usesLineFragmentOrigin)
      let attribute =
      return NSString(string: text).boundingRect(with: size, options: options, attributes: attribute, context: nil)
    }
</code></pre>

<p><strong>如果标签距离领先 20 点,则减去 -:</strong></p>

<pre><code>func estimatedFrameHeight(text:String) -&gt; CGRect{
            let size = CGSize(width: (view.frame.width - 20), height: 1000)
            let options = NSStringDrawingOptions.usesFontLeading.union(.usesLineFragmentOrigin)
            let attribute =
            return NSString(string: text).boundingRect(with: size, options: options, attributes: attribute, context: nil)
      }
</code></pre></p>
                                   
                                                <p style="font-size: 20px;">关于ios - Swift 给 uicollectionviewcell 宽度和动态高度,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/45663870/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/45663870/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - Swift 给 uicollectionviewcell 宽度和动态高度