【问题标题】:Custom multiline UILabel with black background behind text自定义多行 UILabel,文本后面有黑色背景
【发布时间】:2015-04-10 07:33:24
【问题描述】:

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

我试过了:

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

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

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

【问题讨论】:

  • 添加空格怎么样?
  • 我可以在 [Songs 之前和 mp3 之后添加空间,但在 Perfect 之后这是不可能的。具有挑战性的部分是在文本Perfect之后设置填充。
  • @ZoebS 你为什么不直接使用 UITextView?
  • @AlexanderMacLeod 你认为 UITextView 能解决我的问题吗?在我的情况下,使用 UITextView 优于 UIlabel 有什么优势?

标签: ios objective-c iphone ios8


【解决方案1】:

我会使用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;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-22
    • 1970-01-01
    • 2019-09-19
    • 2012-09-22
    相关资源
    最近更新 更多