使用NSMutableAttributedString

时间:2015-12-08 09:55:53

标签: ios objective-c uikit uilabel

我想为突出显示的UILabel添加一些填充(见截图)。 enter image description here

我尝试过像这样继承UILabel而没有成功:

- (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;

0 个答案:

没有答案