ios 调整 UIColor 的 alpha
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30930223/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Adjust alpha of UIColor
提问by Eric
I set a UIColor
using rgb to a background of a UILabel
. I'm trying to adjust the alpha
only. How can I modify the alpha of an existing rgb UIColor
?
我将 a UIColor
using rgb设置为 a 的背景UILabel
。我正在尝试调整alpha
唯一的。如何修改现有 rgb 的 alpha 值UIColor
?
Edit
编辑
Basically I have UILabels
that have a set UIColor
(using rgb), and I won't know what color the UILabels
are. At a certain point, I will have to change the labels
alpha`. How can I just change the labels color alpha?
基本上我UILabels
有一套UIColor
(使用 rgb),我不知道它们是什么颜色UILabels
。在某个时候,我将不得不更改labels
alpha`。我怎样才能改变标签颜色 alpha?
回答by Eric
colorWithAlphaComponent:
did the trick.
colorWithAlphaComponent:
成功了。
So what I had to do was:
所以我必须做的是:
self.mylabel.backgroundColor = [self.myLabel.backgroundColor colorWithAlphaComponent:0.3];
回答by GraSim
Swift 3 & 4
斯威夫特 3 & 4
yourUIView.backgroundColor = UIColor.white.withAlphaComponent(0.75)
Swift 2
斯威夫特 2
yourUIView.backgroundColor = UIColor.whiteColor().colorWithAlphaComponent(0.75)
回答by chris g
If you have custom colors defined, you can do this as well:
如果您定义了自定义颜色,您也可以这样做:
view.backgroundColor = [[MyClass someColor] colorWithAlphaComponent:0.25];
回答by JZAU
why not using label.alpha = 0.5
? to adjust your label's alpha?
为什么不使用label.alpha = 0.5
?调整标签的alpha?
update: if you want to adjust alpha from a old color, here is an example:
更新:如果您想从旧颜色调整 alpha,这是一个示例:
UIColor uicolor = [UIColor greenColor];
CGColorRef color = [uicolor CGColor];
int numComponents = CGColorGetNumberOfComponents(color);
UIColor newColor;
if (numComponents == 4)
{
const CGFloat *components = CGColorGetComponents(color);
CGFloat red = components[0];
CGFloat green = components[1];
CGFloat blue = components[2];
newColor = [UIColor colorWithRed: red green: green blue: blue alpha: YOURNEWALPHA];
}
回答by Santu C
Please set alpha in below code -
请在下面的代码中设置 alpha -
[UIColor colorWithRed:200.0/255.0 green:191.0/255.0 blue:231.0 /255.0 alpha:1.0]