文本后面带有黑色背景的自定义多行 UILabel

Custom multiline UILabel with black background behind text

我需要创建文本后面带有黑色背景的自定义多行 UILabel,并从左、右、上和下填充 2 个像素。所有边的填充必须相同,假设如果 [Songs.. 和黑色边框之间的填充 = 2 那么我需要 "Perfect" 和右边框之间的 2pixel 填充,在 "mp3"

之后相同

我试过这个:

[string addAttribute:NSBackgroundColorAttributeName
          value:[UIColor blackColor]
          range:NSMakeRange(0, string.length)];

但它没有给我额外的填充。

你们能给我建议任何图书馆或一些提示也会有帮助吗

我会使用 UITextView 而不是 UILabel。执行如下操作:

UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(10, y, self.view.frame.size.width - 20, 40)];
textView.textAlignment = NSTextAlignmentJustified;
textView.scrollEnabled = NO;
textView.backgroundColor = [UIColor blackColor];
textView.userInteractionEnabled = NO;