Set alpha layer border?

I set the border for my layer by adding the following line of code:

[self.avatar.layer setBorderColor:[UIColor whiteColor].CGColor];

How to change alpha only on border and not on avatar object?

+5
source share
4 answers

Use UIColor +colorWithWhite:alpha:

 [self.avatar.layer setBorderColor:[UIColor colorWithWhite:1.0f alpha:0.5f].CGColor]; 

If you need a color other than white, use +colorWithRed:green:blue:alpha: as mentioned by user +colorWithRed:green:blue:alpha: .

+10
source

For version Swift-3

  let opacity:CGFloat = 0.6 let borderColor = UIColor.white self.view.layer.borderColor = borderColor.withAlphaComponent(opacity).cgColor 
+3
source

Use [UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:0.5] . A value of 1.0 for red, green, and blue makes the color white, and you can change the alpha to whatever you want.

+2
source

With -colorWithAlphaComponent: you can easily do this for any borderColor

 CGFloat opacity = 0.5f; self.avatar.layer.borderColor = [borderColor colorWithAlphaComponent:opacity].CGColor; 
+2
source

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


All Articles