How to change the alpha channel of an existing background color

I have a UITextField that has rgb already applied to its background color. I am trying to set the alpha of the background color. How to adjust alpha without pasting all rgb color in Objective-c ?

+6
source share
3 answers

You are looking for something like this:

 CGFloat newAlpha = 0.7; CGColorRef color = CGColorCreateCopyWithAlpha(self.yourTextField.layer.backgroundColor, newAlpha); [self.yourTextField.layer setBackgroundColor:color]; 

Or, if you already know the color and want only the current alpha:

 CGFloat alpha = CGColorGetAlpha(self.yourTextField.layer.backgroundColor); //Get current alpha self.yourTextField.layer.backgroundColor = [UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:alpha].CGColor; 
+8
source

You can use:

 UIColor *color = ...; color = [color colorWithAlphaComponent:0.5f]; 
+9
source

In Swift 3 or 4:

 let color: UIColor = UIColor.black.withAlphaComponent(0.7) 
+2
source

Source: https://habr.com/ru/post/985640/


All Articles