我希望为一个高亮的UILabel添加一些内边距(见截图)。
我已经尝试过子类化UILabel,但没有成功:
![enter image description here](https://istack.dev59.com/nJ4xy.webp)
- (id)initWithFrame:(CGRect)frame{
self = [super initWithFrame:frame];
if (self) {
self.edgeInsets = UIEdgeInsetsMake(5, 5, 0, 0); //customize padding here
}
return self;
}
- (void)drawTextInRect:(CGRect)rect {
[super drawTextInRect:UIEdgeInsetsInsetRect(rect, self.edgeInsets)];
}
当前创建标签的功能:
paddingLabel *viewTitleLabel = [[paddingLabel alloc]initWithFrame:CGRectMake(view.frame.origin.x, view.frame.origin.y, view.frame.size.width, view.frame.size.height)];
NSMutableAttributedString *s =
[[NSMutableAttributedString alloc] initWithString:ticketcode];
[s addAttribute:NSBackgroundColorAttributeName
value:[UIColor blackColor]
range:NSMakeRange(0, s.length)];
viewTitleLabel.attributedText = s;