Created
December 1, 2017 10:46
-
-
Save GemmaDelOlmo/44737a1acc5295c7b7318a6d43685863 to your computer and use it in GitHub Desktop.
Extension on CALayer to add borders
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| extension CALayer { | |
| func addBorder(edge: UIRectEdge, color: UIColor, thickness: CGFloat, inset: CGFloat) { | |
| let border = CALayer() | |
| switch edge { | |
| case UIRectEdge.top: | |
| border.frame = CGRect.init(x: inset, y: 0, width: frame.width - inset * 2, height: thickness) | |
| border.name = "top" | |
| case UIRectEdge.bottom: | |
| border.frame = CGRect.init(x: inset, y: frame.height - thickness, width: frame.width - inset * 2, height: thickness) | |
| border.name = "bottom" | |
| case UIRectEdge.left: | |
| border.frame = CGRect.init(x: 0, y: 0, width: thickness, height: frame.height) | |
| border.name = "left" | |
| case UIRectEdge.right: | |
| border.frame = CGRect.init(x: frame.width - thickness, y: 0, width: thickness, height: frame.height) | |
| border.name = "right" | |
| default: | |
| break | |
| } | |
| border.backgroundColor = color.cgColor | |
| self.addSublayer(border) | |
| } | |
| func addAllBorders(color: UIColor, thickness: CGFloat) { | |
| addBorder(edge: .top, color: color, thickness: thickness, inset: 0) | |
| addBorder(edge: .bottom, color: color, thickness: thickness, inset: 0) | |
| addBorder(edge: .left, color: color, thickness: thickness, inset: 0) | |
| addBorder(edge: .right, color: color, thickness: thickness, inset: 0) | |
| } | |
| func removeBorders() { | |
| for layer in sublayers! { | |
| if layer.name == "top" || layer.name == "bottom" || layer.name == "left" || layer.name == "right" { | |
| layer.removeFromSuperlayer() | |
| } | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment