Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save ucwealth/3df5524aafdd8f2df6e3d816c6c5f8af to your computer and use it in GitHub Desktop.
Save ucwealth/3df5524aafdd8f2df6e3d816c6c5f8af 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