Hugo75 Hugo75 - 1 year ago 111
Swift Question

Set darker transparent background to a label on an image

I have an image and a label on it.

To improve readability I would like the transparent background for the label a little bit darker.

Here's an image to figure out what I'm looking for:

enter image description here

Any idea how to perform that? I've already tried with Blur transparency but it's not working.

Answer Source

You can set the background color and transparency for the label in the following way:

label.backgroundColor = UIColor(red: 0, green: 0, blue: 0, alpha: 0.7)

To make your background darker set the alpha (transparency) to a higher value. Completely opaque is an alpha of 1.0 and completely transparent is an alpha of 0.0. So experiment around a bit with the alpha until you find a value you like.

Of course you can also adjust the red, green and blue values to a value of your liking. An RGB of 0,0,0 will make the darkest option, black, which may be the most useful for you here.

Edit: You can also use the following alternatives to achieve the same effect:

label.backgroundColor = UIColor(white: 0.0, alpha: 0.7)


label.backgroundColor = UIColor.blackColor().colorWithAlphaComponent(0.7)

Edit: In Swift 3 this would be:

label.backgroundColor =
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download