我需要创建一个带有背景颜色的UILabel
,并且我想添加一些左/右前导/尾随水平填充。
但是我找到的每个解决方案都像是一个恶心的hack。
从iOS 5开始,有什么“标准”方法来实现这一点?
以下是一个截图来说明我的情况:
我需要创建一个带有背景颜色的UILabel
,并且我想添加一些左/右前导/尾随水平填充。
但是我找到的每个解决方案都像是一个恶心的hack。
从iOS 5开始,有什么“标准”方法来实现这一点?
以下是一个截图来说明我的情况:
要获取所有可用的解决方案,请参见此回答:UILabel文本边距
尝试对UILabel进行子类化,就像@Tommy Herbert在[这个问题][1]的回答中建议的那样。以下为复制并粘贴的代码,以方便您使用:
我通过对UILabel进行子类化并重写drawTextInRect来解决了这个问题,代码如下:
- (void)drawTextInRect:(CGRect)rect {
UIEdgeInsets insets = {0, 5, 0, 5};
[super drawTextInRect:UIEdgeInsetsInsetRect(rect, insets)];
}
super.drawText(in: rect.inset(by: paddingInsets))
。 - parag最重要的部分是,您必须覆盖intrinsicContentSize()
和drawTextInRect()
这两个方法以考虑自动布局:
var contentInset: UIEdgeInsets = .zero {
didSet {
setNeedsDisplay()
}
}
override public var intrinsicContentSize: CGSize {
let size = super.intrinsicContentSize
return CGSize(width: size.width + contentInset.left + contentInset.right, height: size.height + contentInset.top + contentInset.bottom)
}
override public func drawText(in rect: CGRect) {
super.drawText(in: UIEdgeInsetsInsetRect(rect, contentInset))
}
在字符串中添加一个空格字符,这就是“穷人的填充” :)
或者
我会选择自定义背景视图,但如果您不想要那样,我只能看到其他简单的选项是使用空格...
或者编写自定义标签。通过coretext呈现文本。
#define PADDING 5
@interface MyLabel : UILabel
@end
@implementation MyLabel
- (void)drawTextInRect:(CGRect)rect {
UIEdgeInsets insets = UIEdgeInsetsMake(0, PADDING, 0, PADDING);
CGRect rect = UIEdgeInsetsInsetRect(rect, insets);
return [super drawTextInRect:rect];
}
- (CGRect)textRectForBounds:(CGRect)bounds
limitedToNumberOfLines:(NSInteger)numberOfLines
{
CGSize size = CGSizeMake(999, 999);
CGRect rect = [self.attributedText
boundingRectWithSize:size
options:NSStringDrawingUsesLineFragmentOrigin
context:nil];
return CGRectInset(rect, -PADDING, 0);
}
@end
let six_per_em_space = "\u{2006}"
或者,直接从 HTML 页面中将空格复制/粘贴到 Interface Builder 中的 UILabel 文本字段中。
注:附带的图片是截图,不是HTML,所以如果要剪切/粘贴空格,请访问链接页面。
UIView* bg = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width, 70)];
bg.backgroundColor = [UIColor blackColor];
UILabel* yourLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, y, yourWidth, yourHeight)];
[bg addSubview:yourLabel];
[self addSubview:bg];
Swift 5
创建以下类文件并通过故事板将其设置为标签的自定义类名称即可。就是这样。
class PaddingLabel: UILabel {
override func drawText(in rect: CGRect) {
let insets = UIEdgeInsets(top: 0, left: 8, bottom: 0, right: 0)//CGRect.inset(by:)
super.drawText(in: rect.inset(by: insets))
}
}
我看到这里有一些问题,例如当添加填充时,内容的宽度会溢出框,并且我想要一些圆角。我使用以下UILabel子类解决了这个问题:
#import "MyLabel.h"
#define PADDING 8.0
#define CORNER_RADIUS 4.0
@implementation MyLabel
- (void)drawRect:(CGRect)rect {
self.layer.masksToBounds = YES;
self.layer.cornerRadius = CORNER_RADIUS;
UIEdgeInsets insets = {0, PADDING, 0, PADDING};
return [super drawTextInRect:UIEdgeInsetsInsetRect(rect, insets)];
}
- (CGSize) intrinsicContentSize {
CGSize intrinsicSuperViewContentSize = [super intrinsicContentSize] ;
intrinsicSuperViewContentSize.width += PADDING * 2 ;
return intrinsicSuperViewContentSize ;
}
@end
UIEdgeInsets insets = {0, PADDING, 0, PADDING};
转换为:
UIEdgeInsets insets = {PADDING, PADDING, PADDING, PADDING};
在宽度的相似行下面添加这一行:
intrinsicSuperViewContentSize.height += PADDING * 2 ;
我为了解决这个问题,使用了UIButton代替UILabel。然后在Interface Builder的Attributes Inspector中,将Title的Edge用作填充。
如果您不将按钮附加到操作,则单击时它不会被选中,但仍会显示高亮。
您也可以使用以下代码以编程方式完成此操作:
UIButton *mButton = [[UIButton alloc] init];
[mButton setTitleEdgeInsets:UIEdgeInsetsMake(top, left, bottom, right)];
[mButton setTitle:@"Title" forState:UIControlStateNormal];
[self.view addSubView:mButton];
这种方法可以得到相同的结果,但有时候由于某些原因它不起作用,我没有进行调查,因为如果可能的话我会使用Interface Builder。
这仍然是一种解决办法,但如果高亮不影响您的话,它可以很好地工作。希望这有用。
UIButton
用作 UILabel
;它们具有完全不同的用例和不同的功能集。 - Zorayr