ios 如何用白色初始化 UIColor

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8472981/
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-30 15:50:20  来源:igfitidea点击:

How to initialize UIColor with white color

iosobjective-cuicolor

提问by Rakesh Singh

I am trying to initialize UIColorinstance with whiteColorand I am not able to do it. The screen appears black if I do this:

我正在尝试初始化UIColor实例,whiteColor但我无法做到。如果我这样做,屏幕会显示为黑色:

color = [UIColor colorWithWhite:1.0 alpha:1.0];

But below line works fine ...

但下面的线工作正常......

color = [UIColor colorWithRed:197.0/255.0 
                        green:169.0/255.0 
                         blue:140.0/255.0 
                        alpha:1.0];

I am sure I am doing something stupid, any ideas?

我确定我在做一些愚蠢的事情,有什么想法吗?

回答by Fran Sevillano

UIColor *color = [UIColor whiteColor];

回答by phi

Your code color = [UIColor colorWithWhite:1.0 alpha:1.0]should work just fine. Could it be that you forgot a semicolon in the end?

您的代码color = [UIColor colorWithWhite:1.0 alpha:1.0]应该可以正常工作。会不会是你最后忘记了分号?

回答by yanmy

It's very easy, just use this function:

这很简单,只需使用此功能:

UIColor * color = [UIColor colorWithRed:255/255.0f 
                                  green:204/255.0f 
                                   blue:0/255.0f 
                                  alpha:1.0f];

You can use an online tool to get the RGB components or get a code that can be used to initialize UIColor.

您可以使用在线工具获取 RGB 组件或获取可用于初始化 UIColor 的代码。

回答by preynolds

UIColor *color = [UIColor whiteColor];

You can also find a list of all preset component values supported by UIColor objects here.

您还可以在此处找到 UIColor 对象支持的所有预设组件值的列表。

回答by magnusMTB

When using the colorWithWhite method on UIColor, the first parameter is a grayscale value to be applied to the color. Thus, when you provide a 1.0 value, you are turning the color to black (providing an alpha of 1.0). for white, you would provide a 0.0 value for the first parameter.

在 UIColor 上使用 colorWithWhite 方法时,第一个参数是要应用于颜色的灰度值。因此,当您提供 1.0 值时,您将颜色变为黑色(提供 1.0 的 alpha)。对于白色,您将为第一个参数提供 0.0 值。

回答by Krishnadas PC

For Swift 3it is like this

对于Swift 3,它是这样的

UIColor.white

UIColor.white

回答by rvg

Swift 2.x

斯威夫特 2.x

UIColor.whiteColor()

回答by Faheem

Any (CG object) = UIColor.LightGray.CGColor ;