菜鸟教程小白 发表于 2022-12-12 21:59:47

ios - 如何使用自动布局处理 UITableViewCell 内的 UILabel?


                                            <p><p>如何使用自动布局处理 UITableViewCell 中的 UILabel ?根据该标签的长度..这是我的观点:</p>

<p> <a href="/image/5ADMCm.png" rel="noreferrer noopener nofollow"><img src="/image/5ADMCm.png" alt="enter image description here"/></a> </p>

<p>我希望蓝色标签位于中心,但如果它很长,则不要与同一单元格中的相邻标签交叉。</p>

<p>我在它们之间添加了约束,但它不会在中心。</p>

<p>对这种类型的细胞有什么好的限制吗?</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>让我们将标签命名为 <code>title_label</code> 和 <code>detail_label</code>,即在您的第三个单元格中,“服务”是标题,另一个长标签是详细信息。</p>

<p>我。将 title_label 的前导空格设置为 superview 根据您的需要设置常量。并创建它的导出让它成为<code>constraint_title_leading</code>。 </p>

<p>二。 <code>title_label</code> 在容器中垂直居中。 </p>

<p>三。现在将 <code>detail_label</code> 的前导空格设置为 <code>title_label</code> 的尾部。设常数为 8 点。创建一个导出,让它的名字是 <code>constraint_detail_leading</code> </p>

<p>四。将 detail_label 设置为垂直居中 w.r.t <code>title_label</code> 或 superview。 </p>

<p>六。将 <code>detail_label</code> 的内容对齐设置为居中。 </p>

<p>七。将详细标签的尾随空格设置为 superview 不关心它的常量,我们将在代码中更改它。让它的名字是 <code>constraint_detail_trailing</code></p>

<p>八。设置单元格的数据源后调用此方法。</p>

<pre><code>func adjustConstraints() -&gt; Void {


   let titleWidth =   (self.title_label.text! as NSString).boundingRectWithSize(CGSizeMake(600, 600), options: .UsesLineFragmentOrigin,
    attributes: , context: nil).width

    self.constraint_detail_trailing.constant = self.constraint_title_leading.constant + titleWidth + self.constraint_detail_leading.constant
    self.layoutIfNeeded()
}
</code></pre>

<p>启用多行,即行的动态高度,而不是 <code>detail_label</code> 的中心约束将其固定到顶部和底部 w.r.tsuperView ,以便顶部常量等于底部常量并设置其 <code>numberOfLines</code> 为 0。并设置 <code>self.tableView.rowHeight = UITableViewAutomaticDimension</code> </p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 如何使用自动布局处理 UITableViewCell 内的 UILabel?,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/39259107/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/39259107/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 如何使用自动布局处理 UITableViewCell 内的 UILabel?