Skip to content

Instantly share code, notes, and snippets.

@npu3pak
Created October 13, 2018 05:51
Show Gist options
  • Save npu3pak/eaa0f7116c0e59612d6a90085666062b to your computer and use it in GitHub Desktop.
Save npu3pak/eaa0f7116c0e59612d6a90085666062b to your computer and use it in GitHub Desktop.
Round cornered view with border
import UIKit
extension UIView {
func configureBorder(_ roundCorners:UIRectCorner, cornerRadius: CGFloat, borderColor: UIColor, borderWidth: CGFloat) {
let path = UIBezierPath(roundedRect: bounds,
byRoundingCorners: roundCorners,
cornerRadii: CGSize(width: cornerRadius, height: cornerRadius))
let mask = CAShapeLayer()
mask.path = path.cgPath
mask.borderColor = UIColor.white.cgColor
layer.mask = mask
let border = CAShapeLayer()
border.path = path.cgPath
border.lineWidth = borderWidth
border.strokeColor = borderColor.cgColor
border.fillColor = UIColor.clear.cgColor
layer.addSublayer(border)
clipsToBounds = true
}
}
let view = UIView(frame: CGRect(x: 0, y: 0, width: 300, height: 300))
view.backgroundColor = UIColor.red
view.configureBorder([.bottomLeft, .bottomRight], cornerRadius: 30, borderColor: .green, borderWidth: 4)
@npu3pak
Copy link
Author

npu3pak commented Oct 13, 2018

2018-10-13 8 56 50

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment