简体   繁体   中英

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.

您只能使用标签将IBObject写入下一行。

label.backgroundColor = UIColor.black.withAlphaComponent(0.4)

create the UIview with black background color and add the UIlabel inside the view and set the alpha to 0.5 of view . finally you get the black transparent, for eg

在此处输入图片说明

if you need the label text in bold change the view hierarchy

place the UILabel fist then add the UIView.

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.

在此处输入图片说明

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.

在此处输入图片说明

UILabel放在UIView上方,而不是放在其中。

假设我们正在谈论v1包含label的视图,如果v1 另一个具有鲜艳颜色的视图上方 ,则当v1alpha降低时,标签白色文本颜色将变得更难看,该文本颜色没有任何意义。与背景Alpha分量有关,因此您需要重新考虑使用白色或alpha component背景,或者v1后面的视图使其更暗,这就是我们可以说的

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM