ios - 使用自动布局或 layoutMargins 向 UILabel 添加填充?
<p><div>
<aside class="s-notice s-notice__info post-notice js-post-notice mb16"role="status">
<div class="d-flex fd-column fw-nowrap">
<div class="d-flex fw-nowrap">
<div class="flex--item wmn0 fl1 lh-lg">
<div class="flex--item fl1 lh-lg">
<b>这个问题在这里已经有了答案</b>:
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>如果我是你,我会继承 <code>UILabel</code> 并添加 <code>UIEdgeInsets</code>。在您的 <code>UILabel</code> 子类中执行以下操作:</p>
<p>.m 文件</p>
<pre><code> - (id)initWithFrame:(CGRect)frame
{
self = ;
if (self){
//Property in the header file so we can add custom insets per instance of this class
self.edgeInsets = UIEdgeInsetsMake(0, 0, 0, 0);
}
return self;
}
-(void)drawTextInRect:(CGRect)rect
{
;
}
/* So that it will also work with auto layout */
-(CGSize)intrinsicContentSize
{
CGSize size = ;
size.width += self.edgeInsets.left + self.edgeInsets.right;
size.height += self.edgeInsets.top + self.edgeInsets.bottom;
if (self.numberOfLines == 0){
//There is a bug where intrinsice content
//size may be 1 point too short
size.height += 1;
}
return size;
}
</code></pre></p>
<p style="font-size: 20px;">关于ios - 使用自动布局或 layoutMargins 向 UILabel 添加填充?,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/26879944/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/26879944/
</a>
</p>
页:
[1]