菜鸟教程小白 发表于 2022-12-12 20:52:00

ios - 在不使用 TextView 的情况下获取属性字符串的估计高度


                                            <p><p>这个问题偶尔会出现,但我还没有找到可靠的解决方案。</p>

<p>我想准确估计 NSAttributedString 的高度,而不必将文本放入不可见的 TextView 中以获得真实高度。 ( TextView 方法需要更多的处理时间)。但我无法通过使用 boundingRectWithSize 获得始终如一的可靠值。对于我的目的而言,它已经足够接近 10 的 9 倍,但这还不够好,因为它偶尔会导致 View 被截断,其中一行文本不可见。</p>

<p>我的示例代码如下。输出是:</p>

<pre><code>2016-06-27 08:54:17.106 TextHeightTest estimated1225.000000
2016-06-27 08:54:17.108 TextHeightTest estimated2209.000000
</code></pre>

<p>第一个值是正确的。</p>

<p>在.h文件中:</p>

<pre><code>@interface ViewController : UIViewController {
    NSMutableDictionary *attributes;
    UIFont *font;
    UITextView *sizingTextView;
    CGFloat width;
}
</code></pre>

<p>.m 文件中:</p>

<pre><code>- (void)viewDidLoad {
    ;

    sizingTextView = [ init];
    font = ;
    width = 300;

    NSMutableParagraphStyle *paragraphStyle = [ init];
    paragraphStyle.alignment = NSTextAlignmentLeft;
    paragraphStyle.lineSpacing = 7;
    paragraphStyle.maximumLineHeight = 20;
    paragraphStyle.minimumLineHeight = 20;

    attributes = [ init];
    ;
    ;

    NSString *text = @&#34;Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.&#34;;
    NSAttributedString *attribStr = [ initWithString:text attributes:attributes];

    CGFloat estimated1 = ;
    NSLog(@&#34;estimated1%f&#34;, estimated1);

    CGFloat estimated2 = ;
    NSLog(@&#34;estimated2%f&#34;, estimated2);
}

- (CGFloat)estimateInTextView:(NSAttributedString *)text {
    sizingTextView.attributedText = text;
    return .height;
}

- (CGFloat)estimateInSpace:(NSString *)text {
    CGRect rect = ;
    return CGRectGetHeight(rect);

}
</code></pre>

<p>感谢您的帮助。谢谢。</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p> TextView 将文本放入文本容器中。默认情况下,文本容器在顶部和底部插入 8 个点。请参阅 <a href="https://developer.apple.com/library/ios/documentation/UIKit/Reference/UITextView_Class/#//apple_ref/occ/instp/UITextView/textContainerInset" rel="noreferrer noopener nofollow"><code>textContainerInset</code></a> 的文档.因此, TextView 将比文本本身所需的空间高 16 磅。</p>

<p>由于文本容器的 <code>lineFragmentPadding</code>(默认为左右 5 点),也可能存在基于宽度的换行差异。不过,我不确定。</p>

<p>对于测试,您实际上可以在给定大小的矩形中绘制字符串,并将其与在相同大小的 TextView 中呈现的字符串进行比较。查看线条是否在不同的点断开。在两者周围画一个框架,看看文本在哪里插入不同。</p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 在不使用 TextView 的情况下获取属性字符串的估计高度,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/38056060/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/38056060/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 在不使用 TextView 的情况下获取属性字符串的估计高度