使用 NSMutableAttributedString 向 UILabel 添加填充

问题描述 投票:0回答:1

我想向突出显示的 UILabel 添加一些填充(参见屏幕截图)。

我尝试像这样对 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;
ios objective-c uikit uilabel
1个回答
0
投票

左缩进的快速解决方案:

let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.firstLineHeadIndent = 10
paragraphStyle.headIndent = 10
let attributedString = NSMutableAttributedString(string: string, attributes: attributes)
© www.soinside.com 2019 - 2024. All rights reserved.