简体   繁体   English

将UILabel的背景设置为透明黑色

[英]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. 我有一个UILabel,我想将其背景设置为带有alpha分量的黑色,以便此标签可以具有透明的黑色背景。

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. 在情节提要中,当我将背景设置为想要的带有alpha分量的颜色时,即使白色标签的文本也随着alpha分量变得更亮。

To overcome this problem I embedded the label in a UIView and even then the same problem persists. 为了克服这个问题,我将标签嵌入到UIView中,即使这样,同样的问题仍然存在。

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 . 创建具有黑色背景色的UIview并在视图内部添加UIlabel并将alpha设置为view的0.5 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. 放置UILabel拳头,然后添加UIView。

Instead of changing the alpha value, change the opacity of the label background color in storyboard. 而不是更改alpha值,而是在情节提要中更改标签背景颜色的不透明度。

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. 为了更好地理解,我拍摄了一个imageView并将一个标签放置在imageView的底部,并将标签和textColor的背景色更改为白色。 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. 在这里,我将其设置为50%,您可以将其更改为0到100之间的任意值,直到获得所需的透明度为止。

在此处输入图片说明

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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