我需要创建一个UILabel背景颜色,我想添加一些左边填充,但我在这里找到的每个解决方案似乎都是一个讨厌的黑客.
从iOS 5开始实现这一目标的"标准"方法是什么?
编辑
用于说明我的场景的屏幕截图.-

sie*_*ous 94
有关可用解决方案的完整列表,请参阅以下答案:UILabel文本边距
尝试继承UILabel,就像@Tommy Herbert在[this question] [1]的回答中所建议的那样.复制并粘贴以方便您:
我通过继承UILabel并覆盖drawTextInRect来解决这个问题:像这样:
- (void)drawTextInRect:(CGRect)rect {
UIEdgeInsets insets = {0, 5, 0, 5};
[super drawTextInRect:UIEdgeInsetsInsetRect(rect, insets)];
}
Run Code Online (Sandbox Code Playgroud)
Mik*_*ike 25
我知道这是旧的,但后人...
最重要的部分是你必须覆盖intrinsicContentSize()和drawTextInRect()以便考虑AutoLayout
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))
}
Run Code Online (Sandbox Code Playgroud)
Dai*_*jan 24
字符串也添加一个空格字符.这是穷人的填充:)
要么
我会使用自定义背景视图,但如果你不想这样,那么空间是我看到的唯一其他简单选项......
或者写一个自定义标签.通过coretext渲染文本
rud*_*nsm 23
#define PADDING 5
@interface MyLabel : UILabel
@end
@implementation MyLabel
- (void)drawTextInRect:(CGRect)rect {
return [super drawTextInRect:UIEdgeInsetsInsetRect(rect, UIEdgeInsetsMake(0, PADDING, 0, PADDING))];
}
- (CGRect)textRectForBounds:(CGRect)bounds limitedToNumberOfLines:(NSInteger)numberOfLines
{
return CGRectInset([self.attributedText boundingRectWithSize:CGSizeMake(999, 999)
options:NSStringDrawingUsesLineFragmentOrigin
context:nil], -PADDING, 0);
}
@end
Run Code Online (Sandbox Code Playgroud)
inc*_*iko 16
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];
Run Code Online (Sandbox Code Playgroud)
cle*_*ght 15
有时在原型设计时使用UNICODE部分空间来实现对齐是很方便的.这在原型设计,概念验证或仅推迟图形算法的实现方面非常方便.
如果为方便起见使用UNICODE空格,请注意至少有一个UNICODE空格的大小基于它显示的字体,特别是实际空格字符本身(U + 0020,ASCII 32)
如果您在UILabel中使用默认的iOS系统字体,则默认的系统字体特征可能会在后续iOS版本中发生变化,并通过更改应用程序的精确间距突然引入不必要的错位.这可以而且确实发生,例如"旧金山"字体取代了iOS版本中以前的iOS系统字体.
UNICODE易于在Swift中指定,例如:
let six_per_em_space = "\u{2006}"
Run Code Online (Sandbox Code Playgroud)
或者,将HTML页面中的空间直接剪切/粘贴到Interface Builder中的UILabel文本字段中.
注意:附加图片是屏幕截图,而不是 HTML,因此如果要剪切/粘贴空间,请访问链接页面.
我在这里的答案有几个问题,例如当您添加填充时,内容的宽度溢出了框,我想要一些角半径。我使用以下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
Run Code Online (Sandbox Code Playgroud)
希望这对某人有帮助!请注意,如果要在顶部和底部填充,则需要更改以下行:
UIEdgeInsets insets = {0, PADDING, 0, PADDING};
Run Code Online (Sandbox Code Playgroud)
对此:
UIEdgeInsets insets = {PADDING, PADDING, PADDING, PADDING};
Run Code Online (Sandbox Code Playgroud)
并将此行添加到类似的宽度下面:
intrinsicSuperViewContentSize.height += PADDING * 2 ;
Run Code Online (Sandbox Code Playgroud)
小智 5
斯威夫特 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))
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
105312 次 |
| 最近记录: |