菜鸟教程小白 发表于 2022-12-13 11:33:00

ios - UITableViewCell 的 detailTextLabel 中的左对齐和右对齐文本


                                            <p><p>在我的 <code>UITableViewCellStyleSubtitle</code> 的 <code>UITableViewCell</code> 中,我想填充 <code>detailTextLabel</code> 的完整最大宽度;左侧的一个短文本(左对齐)和最右侧的另一个短文本(右对齐)。这两个文本将在它们之间留出足够的空间。</p>

<p>我找到了 <a href="https://stackoverflow.com/a/22618906/1971013" rel="noreferrer noopener nofollow">this</a>但这似乎不起作用;它只是在左右部分之间添加一个固定的空间。</p>

<p>如果 <code>detailTextLabel</code> 有一个固定的宽度分布整个可用单元格空间,这不会那么难:可以计算两个字符串的宽度并使用 <code>NSKernAttributeName 填充剩余空间</code> 例如。但是,不幸的是 <code>detailTextLabel</code> 是针对当前内容动态调整的。</p>

<p>有没有人想出一个优雅的解决方案来做到这一点? (除了小步骤使文本越来越长,直到达到 <code>detailTextLabel</code> 的全部潜在宽度并因为 <code>...</code> 而稍微回缩。)</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>这对我有用。试试这个:</p>

<pre><code> cell.detailTextLabel!.lineBreakMode = NSLineBreakMode.ByWordWrapping;
cell.detailTextLabel!.numberOfLines = 0;
</code></pre>

<p>如果要在tableView中设置左 View 和右 View 。</p>

<p>选择您的 tableView 并转到属性检查器并将原型(prototype)单元格设置为 1,如下面的屏幕截图所示:</p>

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

<p>选择您的 tableView 单元格并进入属性检查器并按照以下屏幕截图所示进行设置:</p>

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

<p>选择正确的细节后,表格 View 单元格将包含以下屏幕截图:</p>

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

<p>“cell.textLabel!.text”中设置的文本是左 View (Title)
而“cell.detailTextLabel!.text”中设置的文本就是右 View (Detail)</p>

<p>希望这可能会有所帮助。</p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - UITableViewCell 的 detailTextLabel 中的左对齐和右对齐文本,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/32106172/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/32106172/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - UITableViewCell 的 detailTextLabel 中的左对齐和右对齐文本