views:

29

answers:

1

Hello. I'm diving into iOS development and I'm programmatically creating some labels, but I can't seem to set their background color to black. From what I read, it seems simple enough, here's my code...

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

When I do this, the border color and width work as expected, but the background color remains white. Am I forgetting to do something?

Thanks so much for your help!

+2  A: 

A UILabel already has a .backgroundColor property, you don't need to tweak its layer...

lbl.backgroundColor = [UIColor blackColor];
KennyTM
thanks, that worked. would you happen to know why the other approach doesn't work?
BeachRunnerJoe
@Beach: No. Probably the label's backgroundColor is drawn above the layer, as you can reveal the layer's backgroundColor by setting the label's backgroundColor to `[UIColor clearColor]`.
KennyTM