Created
June 23, 2021 10:52
-
-
Save IniongunIsaac/510ce2253d6335d10e4b8e5ebbb56c4e to your computer and use it in GitHub Desktop.
View with dashed borders
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
import UIKit | |
class RectangularDashedView: UIView { | |
@IBInspectable var viewCornerRadius: CGFloat = 0 { | |
didSet { | |
layer.cornerRadius = viewCornerRadius | |
layer.masksToBounds = viewCornerRadius > 0 | |
} | |
} | |
@IBInspectable var dashWidth: CGFloat = 0 | |
@IBInspectable var dashColor: UIColor = .clear | |
@IBInspectable var dashLength: CGFloat = 0 | |
@IBInspectable var betweenDashesSpace: CGFloat = 0 | |
var dashBorder: CAShapeLayer? | |
override func layoutSubviews() { | |
super.layoutSubviews() | |
dashBorder?.removeFromSuperlayer() | |
let dashBorder = CAShapeLayer() | |
dashBorder.lineWidth = dashWidth | |
dashBorder.strokeColor = dashColor.cgColor | |
dashBorder.lineDashPattern = [dashLength, betweenDashesSpace] as [NSNumber] | |
dashBorder.frame = bounds | |
dashBorder.fillColor = nil | |
if viewCornerRadius > 0 { | |
dashBorder.path = UIBezierPath(roundedRect: bounds, cornerRadius: viewCornerRadius).cgPath | |
} else { | |
dashBorder.path = UIBezierPath(rect: bounds).cgPath | |
} | |
layer.addSublayer(dashBorder) | |
self.dashBorder = dashBorder | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment