我目前正在这样做:
UIColor *myColor = [UIColor clearColor];
这很棒,但我想指定某个“myColor”的alpha。我该怎么做?
答案 0 :(得分:127)
如果您有现有颜色,则可以返回具有指定alpha的新颜色,如下所示:
- (void)setBackgroundColor:(UIColor *)color
{
self.backgroundColor = [color colorWithAlphaComponent:0.3f];
}
答案 1 :(得分:12)
[UIColor clearColor]
,我该怎么说呢?明白!
这是一个方便类方法,返回UIColor
,其alpha值为零。
如果您想要一种具有分数透明度的颜色:
+ (UIColor *)colorWithRed:(CGFloat)red green:(CGFloat)green blue:(CGFloat)blue alpha:(CGFloat)alpha
或其他UIColor
类方法之一。
答案 2 :(得分:12)
你可以这样使用colorWithWhite:alpha
:
[UIColor colorWithWhite:1.0 alpha:0.5];
或者如果你想要一种特定的颜色:
[UIColor colorWithRed:1.0 green:1.0 blue:0.3 alpha:0.72];
查看the docs。
答案 3 :(得分:7)
对于Swift,您可以使用:
Private Function getfiledata(ByVal fichier As String) As String
Dim fileReader As String
Dim FichierFinal As String
MsgBox(fichier)
FichierFinal = fichier.Replace("E002.pfx","_E002.pem")
FichierFinal = fichier.Replace("X002.pfx","_X002.pem")
FichierFinal = fichier.Replace("A005.pfx","_A005.pem")
MsgBox(FichierFinal)
fileReader = My.Computer.FileSystem.ReadAllText(FichierFinal)
Return fileReader
End Function
或RGB颜色:
UIColor.black.withAlphaComponent(0.5)
答案 4 :(得分:1)
回答这个问题
UIColor *myColor = [[UIColor clearColor] colorWithAlphaComponent:0.3f];
我认为你有" clearColor"作为有效的UIColor;
@refer James O' Brien Solution;
答案 5 :(得分:0)
如果您更喜欢Swift中的简单但强大解决方案,请结帐 HandyUIKit 。使用Carthage 在项目中安装它 - 然后您的生活变得更轻松:
import HandyUIKit
// creates a new UIColor object with the given value set
myColor.change(.alpha, to: 0.2)
还可以选择应用相对更改:
// create a new UIColor object with alpha increased by 0.2
myColor.change(.alpha, by: 0.2)
我希望它有所帮助!