Ios 使用NSMutableAttributeString向UILabel添加填充
我想给突出显示的UILabel添加一些填充(参见屏幕截图)。 我尝试过这样子类化UILabel,但没有成功:Ios 使用NSMutableAttributeString向UILabel添加填充,ios,objective-c,uikit,uilabel,Ios,Objective C,Uikit,Uilabel,我想给突出显示的UILabel添加一些填充(参见屏幕截图)。 我尝试过这样子类化UILabel,但没有成功: - (id)initWithFrame:(CGRect)frame{ self = [super initWithFrame:frame]; if (self) { self.edgeInsets = UIEdgeInsetsMake(5, 5, 0, 0); //customize padding here } return self;
- (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;
添加填充到标签?你的意思是图像前面需要一些空间?更像是四个侧面的插图