向 UILabel 添加空间/填充 [英] Adding space/padding to a UILabel
问题描述
我有一个 UILabel
,我想在顶部和底部添加空间.使用约束中的最小高度,我已将其修改为:
I have a UILabel
where I want to add space in the top and in the bottom. With the minimum height in constraints, I've modified it to:
为此我使用了:
override func drawTextInRect(rect: CGRect) {
var insets: UIEdgeInsets = UIEdgeInsets(top: 0.0, left: 10.0, bottom: 0.0, right: 10.0)
super.drawTextInRect(UIEdgeInsetsInsetRect(rect, insets))
}
但我必须找到不同的方法,因为如果我写了两行以上,问题是一样的:
But I've to find a different method because if I write more than two lines, the problem is the same:
推荐答案
如果你想坚持使用 UILabel,而不是继承它,Mundi 为您提供了明确的解决方案.
If you want to stick with UILabel, without subclassing it, Mundi has given you a clear solution.
如果您愿意避免使用 UIView 包装 UILabel,则可以使用 UITextView 来启用 UIEdgeInsets(填充)或子类 UILabel 以支持 UIEdgeInsets.
If alternatively, you would be willing to avoid wrapping the UILabel with a UIView, you could use UITextView to enable the use of UIEdgeInsets (padding) or subclass UILabel to support UIEdgeInsets.
使用 UITextView 只需要提供插图 (Objective-C):
Using a UITextView would only need to provide the insets (Objective-C):
textView.textContainerInset = UIEdgeInsetsMake(10, 0, 10, 0);
替代方案,如果您子类化 UILabel,则此方法的示例将覆盖 drawTextInRect 方法
(目标-C)
Alternative, if you subclass UILabel, an example to this approach would be overriding the drawTextInRect method
(Objective-C)
- (void)drawTextInRect:(CGRect)uiLabelRect {
UIEdgeInsets myLabelInsets = {10, 0, 10, 0};
[super drawTextInRect:UIEdgeInsetsInsetRect(uiLabelRect, myLabelInsets)];
}
您还可以为新的子类 UILabel 提供 TOP、LEFT、BOTTOM 和 RIGHT 的插入变量.
You could additionally provide your new subclassed UILabel with insets variables for TOP, LEFT, BOTTOM and RIGHT.
示例代码可以是:
在 .h (Objective-C)
float topInset, leftInset,bottomInset, rightInset;
在 .m (Objective-C)
- (void)drawTextInRect:(CGRect)uiLabelRect {
[super drawTextInRect:UIEdgeInsetsInsetRect(uiLabelRect, UIEdgeInsetsMake(topInset,leftInset,bottomInset,rightInset))];
}
据我所知,在子类化 UILabel 时,您似乎必须覆盖其内在内容大小.
From what I have seen, it seems you have to override the intrinsicContentSize of the UILabel when subclassing it.
所以你应该像这样覆盖intrinsicContentSize:
So you should override intrinsicContentSize like:
- (CGSize) intrinsicContentSize {
CGSize intrinsicSuperViewContentSize = [super intrinsicContentSize] ;
intrinsicSuperViewContentSize.height += topInset + bottomInset ;
intrinsicSuperViewContentSize.width += leftInset + rightInset ;
return intrinsicSuperViewContentSize ;
}
并添加以下方法来编辑您的插图,而不是单独编辑它们:
And add the following method to edit your insets, instead of editing them individually:
- (void) setContentEdgeInsets:(UIEdgeInsets)edgeInsets {
topInset = edgeInsets.top;
leftInset = edgeInsets.left;
rightInset = edgeInsets.right;
bottomInset = edgeInsets.bottom;
[self invalidateIntrinsicContentSize] ;
}
它将更新 UILabel 的大小以匹配边缘插入并涵盖您提到的多行必要性.
It will update the size of your UILabel to match the edge insets and cover the multiline necessity you referred to.
经过一番搜索后,我发现了这个带有 IPInsetLabel 的 Gist.如果这些解决方案都不起作用,您可以尝试一下.
After searching a bit I have found this Gist with an IPInsetLabel. If none of those solutions work you could try it out.
关于这个问题有一个类似的问题(重复).
有关可用解决方案的完整列表,请参阅此答案:UILabel text margin
There was a similar question (duplicate) about this matter.
For a full list of available solutions, see this answer: UILabel text margin
这篇关于向 UILabel 添加空间/填充的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!