经常会遇到给控件添加边框的需求:边框大体有两种实线和虚线
1、虚线边框的添加:
func clipDashedLine() {
let borderLayer = CAShapeLayer()
borderLayer.bounds = self.bounds
borderLayer.position = CGPoint(x: self.bounds.midX, y: self.bounds.midY)
borderLayer.path = UIBezierPath.init(rect: borderLayer.bounds).cgPath
borderLayer.lineWidth = 1
borderLayer.lineDashPattern = [3,3]
borderLayer.fillColor = UIColor.clear.cgColor
borderLayer.strokeColor = UIColor.red.cgColor
self.layer.addSublayer(borderLayer)
}
2、实现边框的添加:
a、最常采用的方式:
view.layer.borderWidth = 1
view.layer.borderColor = UIColor.red.cgColor
b、其它方式:
func clipFullLine() {
let borderLayer = CAShapeLayer()
borderLayer.bounds = self.bounds
borderLayer.position = CGPoint(x: self.bounds.midX, y: self.bounds.midY)
borderLayer.path = UIBezierPath.init(rect: borderLayer.bounds).cgPath
borderLayer.lineWidth = 1
borderLayer.lineDashPattern = nil
borderLayer.fillColor = UIColor.clear.cgColor
borderLayer.strokeColor = UIColor.red.cgColor
self.layer.addSublayer(borderLayer)
}
该方式和虚线可以结合使用
网友评论