我正在深入研究iOS开发,我正在以编程方式创建一些标签,但我似乎无法将其背景颜色设置为黑色。从我读到的,看起来很简单,这是我的代码......
UILabel *lbl = [[[UILabel alloc] initWithFrame:CGRectMake(x, y, width, height] autorelease];
[[lbl layer] setBorderColor:[[UIColor blackColor] CGColor]];
[[lbl layer] setBorderWidth:1.0];
[[lbl layer] setBackgroundColor:[[UIColor blackColor] CGColor]];
[[self view] addSubview:lbl];
当我这样做时,边框颜色和宽度按预期工作,但背景颜色保持白色。我忘了做某事吗?
非常感谢你的帮助!
答案 0 :(得分:18)
UILabel已经有.backgroundColor
属性,您无需调整其图层......
lbl.backgroundColor = [UIColor blackColor];
答案 1 :(得分:1)
设置UILabel的背景颜色
[lbl.backgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"color_lightblue.png"]]];
使用图像颜色设置UILabel的TextColor
[label_Description setTextColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"color_lightgray.png"]]];
简单的背景色
lbl.backgroundColor = [UIColor blackColor];
答案 2 :(得分:0)
swift 2
label.backgroundColor = UIColor.whiteColor()
其他公式:
let label = UILabel(frame: CGRectMake(0, 0, view.frame.width, view.frame.height))
label.backgroundColor = UIColor.whiteColor()
label.textAlignment = NSTextAlignment.Center
label.text = message
label.numberOfLines = 2
label.font = UIFont.systemFontOfSize(12)
答案 3 :(得分:0)
lbl.backgroundColor = [UIColor blackColor];
答案 4 :(得分:0)
with one code you can set color
LabelObj.backgroundColor = [UIColor blackColor];