Skip to content

Instantly share code, notes, and snippets.

@IniongunIsaac
Created June 23, 2021 10:52
Show Gist options
  • Save IniongunIsaac/510ce2253d6335d10e4b8e5ebbb56c4e to your computer and use it in GitHub Desktop.
Save IniongunIsaac/510ce2253d6335d10e4b8e5ebbb56c4e to your computer and use it in GitHub Desktop.
View with dashed borders
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