Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set the background of UILabel to transparent black

I have a UILabel that whose background I want to set to black with alpha component so that this label can have a transparent black background.

In the storyboard when I set the background to the colour I want with an alpha component even the text of the label that is in white colour becomes lighter with the alpha component.

To overcome this problem I embedded the label in a UIView and even then the same problem persists.

How can I overcome this problem. Any help will be appreciated.

like image 497
A.S Avatar asked Jan 29 '26 02:01

A.S


2 Answers

You can Do only With label make IBObject an write following line.

label.backgroundColor = UIColor.black.withAlphaComponent(0.4)
like image 86
anshul king Avatar answered Jan 31 '26 20:01

anshul king


Instead of changing the alpha value, change the opacity of the label background color in storyboard.

For better understanding, I have taken an imageView and placed a label at the bottom of imageView and changed the background color of the label and textColor to white. At this stage you can't see the image behind the label.

enter image description here

Now, go to attributes inspector and select the label background color. There you can see the opacity and change it's value. Here I have made it 50%, you can change it to whatever you want between 0 to 100 until you get your desired transparency.

enter image description here

like image 30
Venkatesh Chejarla Avatar answered Jan 31 '26 18:01

Venkatesh Chejarla



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!