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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 06:29:51  来源:igfitidea点击:

Adjust alpha of UIColor

iosobjective-calphauicolor

提问by Eric

I set a UIColorusing rgb to a background of a UILabel. I'm trying to adjust the alphaonly. How can I modify the alpha of an existing rgb UIColor?

我将 a UIColorusing rgb设置为 a 的背景UILabel。我正在尝试调整alpha唯一的。如何修改现有 rgb 的 alpha 值UIColor

Edit

编辑

Basically I have UILabelsthat have a set UIColor(using rgb), and I won't know what color the UILabelsare. At a certain point, I will have to change the labelsalpha`. How can I just change the labels color alpha?

基本上我UILabels有一套UIColor(使用 rgb),我不知道它们是什么颜色UILabels。在某个时候,我将不得不更改labelsalpha`。我怎样才能改变标签颜色 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]