I am looking to add a black label with a transparent background to my view (see below)
// ADD LABEL
UILabel *label = [[UILabel alloc] init];
[label setFrame:CGRectMake(124, 312, 72, 35)];
[label setText:@"Yay!"];
[label setTextAlignment:UITextAlignmentCenter];
[[self view] addSubview:label];
[label release];
When I add the label it always comes out black text on a white background. I have looked in the NIB and the only way I can see to make this work is set the background > color > opacity to 0, or in Xcode:
[label setBackgroundColor:[UIColor clearColor]];
Is this the right way to do this?
Cheers Gary
I can confirm that creating a label programatically with background color set using
[label setBackgroundColor:[UIColor clearColor]];
will work. I'm using this in one of my apps.
Also, when you set the background color in Interface Builder, you can set the opacity of the background color to 0. Make sure that you don't set the opacity of the label itself to 0, or the text will also be transparent.
Swift 3 and 4:
yourView.backgroundColor = UIColor.clear
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With