Created
August 24, 2016 18:39
-
-
Save Sorix/259e4cc4e6d0ae30e554f135476853e1 to your computer and use it in GitHub Desktop.
Round specified corners of UIView
This file contains 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
// Example: view.round([.TopLeft, .TopRight], radius: 15) | |
extension UIView { | |
/** | |
Rounds the given set of corners to the specified radius | |
- parameter corners: Corners to round | |
- parameter radius: Radius to round to | |
*/ | |
func round(corners: UIRectCorner, radius: CGFloat) { | |
_round(corners, radius: radius) | |
} | |
/** | |
Rounds the given set of corners to the specified radius with a border | |
- parameter corners: Corners to round | |
- parameter radius: Radius to round to | |
- parameter borderColor: The border color | |
- parameter borderWidth: The border width | |
*/ | |
func round(corners: UIRectCorner, radius: CGFloat, borderColor: UIColor, borderWidth: CGFloat) { | |
let mask = _round(corners, radius: radius) | |
addBorder(mask, borderColor: borderColor, borderWidth: borderWidth) | |
} | |
/** | |
Fully rounds an autolayout view (e.g. one with no known frame) with the given diameter and border | |
- parameter diameter: The view's diameter | |
- parameter borderColor: The border color | |
- parameter borderWidth: The border width | |
*/ | |
func fullyRound(diameter: CGFloat, borderColor: UIColor, borderWidth: CGFloat) { | |
layer.masksToBounds = true | |
layer.cornerRadius = diameter / 2 | |
layer.borderWidth = borderWidth | |
layer.borderColor = borderColor.CGColor; | |
} | |
private func _round(corners: UIRectCorner, radius: CGFloat) -> CAShapeLayer { | |
let path = UIBezierPath(roundedRect: bounds, byRoundingCorners: corners, cornerRadii: CGSize(width: radius, height: radius)) | |
let mask = CAShapeLayer() | |
mask.path = path.CGPath | |
self.layer.mask = mask | |
return mask | |
} | |
private func addBorder(mask: CAShapeLayer, borderColor: UIColor, borderWidth: CGFloat) { | |
let borderLayer = CAShapeLayer() | |
borderLayer.path = mask.path | |
borderLayer.fillColor = UIColor.clearColor().CGColor | |
borderLayer.strokeColor = borderColor.CGColor | |
borderLayer.lineWidth = borderWidth | |
borderLayer.frame = bounds | |
layer.addSublayer(borderLayer) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment