菜鸟教程小白 发表于 2022-12-12 22:57:04

objective-c - UIImageView 文本中的 UILabel 被截断


                                            <p><p>我将 <code>UILabel</code> 放在自定义 <code>UIImageView</code> 中,文本被截断:</p>

<p> <img src="/image/3KzFn.png" alt="screenshot"/> </p>

<p>代码:</p>

<pre><code>//self.frame.size.width and height are the same value, self is a square
self.displayNameLabel = [ initWithFrame:CGRectMake(0, 0, (self.frame.size.width - 12), (self.frame.size.height - 12))];
self.displayNameLabel.center = CGPointMake((self.frame.size.width / 2), (self.frame.size.height / 2));
self.displayNameLabel.backgroundColor = ;
self.displayNameLabel.textAlignment = UITextAlignmentCenter;
self.displayNameLabel.font = ;
self.displayNameLabel.adjustsFontSizeToFitWidth = YES;
</code></pre>

<p>我将字体大小设置为大,然后使用 <a href="https://developer.apple.com/documentation/uikit/uilabel/1620546-adjustsfontsizetofitwidth" rel="noreferrer noopener nofollow"><code>adjustsFontSizeToFitWidth</code></a>以确保它尽可能大。它指出:</p>

<blockquote>
<p>If this property is set to YES, however, and the text in the text property exceeds the label’s bounding rectangle, the receiver starts reducing the font size until the string fits</p>
</blockquote>

<p>如果我将 label.frame 设置为:</p>

<pre><code>self.displayNameLabel = [ initWithFrame:CGRectMake(0, 0, (self.frame.size.width - 0), (self.frame.size.height - 0))];
</code></pre>

<p>然后它按预期工作。 </p>

<p>如何将框架设置为:</p>

<pre><code>self.frame.size.width
self.frame.size.height
</code></pre>

<p>代替:</p>

<pre><code>self.frame.size.width - 14
self.frame.size.height - 14
</code></pre>

<p>工作?</p>

<p>为什么会被夹在上面?</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p><code>adjustsFontSizeToFitWidth</code> 属性只会导致标签调整字体大小,直到字符串的宽度适合框架。如果标签的高度不足以容纳最终的字体大小,它将被剪裁。</p></p>
                                   
                                                <p style="font-size: 20px;">关于objective-c - UIImageView 文本中的 UILabel 被截断,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/12710980/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/12710980/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: objective-c - UIImageView 文本中的 UILabel 被截断