Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UILabel Setting Transparent Background Color?

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

like image 712
fuzzygoat Avatar asked Sep 20 '10 16:09

fuzzygoat


2 Answers

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.

like image 97
Greg Avatar answered Nov 11 '22 19:11

Greg


Swift 3 and 4:

yourView.backgroundColor = UIColor.clear
like image 20
Naishta Avatar answered Nov 11 '22 18:11

Naishta