菜鸟教程小白 发表于 2022-12-11 20:35:48

iphone - UILabel 中的行数


                                            <p><p>我有带有自定义单元格的 UITableView,其中包含 uilabels。
我用它来显示评论,它必须有灵活的高度。
我该怎么做?
我有代码,它在 uilabel.text 长度的帮助下计算行数,但这是错误的方法。你有什么想法吗?</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>使用 UILabel 并将 numberOfLines 设置为零。</p>

<p>然后,为了确保您的标签确实适合单元格,请像这样从 NSString UIKit 扩展中获取尺寸;</p>

<pre><code>CGFloat cellWidth = 320.0f; //example width....
labelSize = ;
</code></pre>

<p>来自 NSString 引用;
sizeWithFont:constrainedToSize:</p>

<blockquote>
<p>Returns the size of the string if it were rendered and constrained to
the specified size.</p>

<p><code>- (CGSize)sizeWithFont:(UIFont *)font constrainedToSize:(CGSize)size</code> </p>

<p>Parameters font The font to use for computing the string size. size
The maximum acceptable size for the string. This value is used to
calculate where line breaks and wrapping would occur. Return Value The
width and height of the resulting string’s bounding box.</p>
</blockquote></p>
                                   
                                                <p style="font-size: 20px;">关于iphone - UILabel 中的行数,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/8285954/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/8285954/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: iphone - UILabel 中的行数