菜鸟教程小白 发表于 2022-12-13 07:27:21

ios - 似乎无法为 UILabel 计算正确的 NSAttributedString 行高


                                            <p><p>我正在使用 <code>NSAttributedString's</code> <code>boundingRectWithSize:options:context:</code> 方法来计算某些文本的预期高度,在阅读 <a href="http://www.objc.io/issue-9/string-rendering.html" rel="noreferrer noopener nofollow">this article from objc.io</a> 之后</p>

<pre><code>+(CGSize)postLabelSizeForPost:(ANKPost *)post
{
CGFloat labelWidth = 220.0f;
NSAttributedString *text = ;
NSStringDrawingOptions options = NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading;
CGRect boundingRect = [text boundingRectWithSize:CGSizeMake(labelWidth, CGFLOAT_MAX)
                                       options:options
                                       context:nil];
CGFloat height = (CGFloat) (ceil(boundingRect.size.height));

return CGSizeMake(labelWidth, height);
}
</code></pre>

<p>但是,当我使用这种方法时,文本实际上并不适合这个大小 - 大小总是太短。 </p>

<p>起初,我认为问题出在表情符号字符上 - 但应用程序中的许多 tableviewcells 似乎都发生了这种情况。</p>

<p><strong>更新:</strong>
我和几个人谈过,听起来这个问题可能是 <code>boundingRectWithSize:options:context:</code> 的潜在错误。我想出了一个解决方法:创建一个虚拟 <code>UILabel</code>,赋予它与 tableview 单元格中的 <code>UILabel</code> 相同的属性(<code>numberOfLines</code> 等)和然后使用 <code>sizeThatFits:</code> 来计算尺寸。可以肯定的是,这是一个 hack - 但它充分解决了我的问题。不过,我希望它更优雅!</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>我在尝试计算 UITextView 的属性文本大小时遇到​​了同样的效果。我遇到了<a href="https://stackoverflow.com/a/18859917/2806963" rel="noreferrer noopener nofollow">this answer to another question</a> .</p>

<p>诀窍是使用 <a href="https://developer.apple.com/library/mac/documentation/Cocoa/Reference/ApplicationKit/Classes/NSLayoutManager_Class/Reference/Reference.html#//apple_ref/occ/instm/NSLayoutManager/usedRectForTextContainer:" rel="noreferrer noopener nofollow">NSLayoutManager&#39;s <code>usedRectForTextContainer:</code></a> .巧妙的是,此方法不需要与 UITextView 结合使用。这意味着您可以在 mainThread 之外使用它,这是预先计算 UITableViewCell 高度的关键。</p>

<p>我是这样使用它的:</p>

<pre><code>- (CGSize)rectForAttributedString:(NSAttributedString *)string withSize:(CGSize)theSize
{
    if (!string || CGSizeEqualToSize(theSize, CGSizeZero)) {
      return CGSizeZero;
    }

    // setup TextKit stack
    NSTextContainer *textContainer = [ initWithSize:theSize];
    NSTextStorage *textStorage = [ initWithAttributedString:string];
    NSLayoutManager *layoutManager = [ init];
    ;
    ;

    // query for size
    CGRect rect = ;

    return CGSizeMake(ceilf(rect.size.width), ceilf(rect.size.height));
}
</code></pre>

<p>为了方便起见,我创建了一个方便的方法来计算特定宽度的高度:</p>

<pre><code>- (CGFloat)textViewHeightForAttributedString:(NSAttributedString *)string withWidth:(CGFloat)width
{
    return .height;
}
</code></pre>

<p>希望这会有所帮助。</p>

<hr/>

<p><em>PS。我对整个编程有点陌生,所以如果我的代码可以优化,请告诉我。</em></p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 似乎无法为 UILabel 计算正确的 NSAttributedString 行高,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/22916566/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/22916566/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 似乎无法为 UILabel 计算正确的 NSAttributedString 行高