美文网首页
给控件添加边框

给控件添加边框

作者: 海到尽头天为岸 | 来源:发表于2018-04-27 14:08 被阅读13次

经常会遇到给控件添加边框的需求:边框大体有两种实线和虚线

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)
   }

该方式和虚线可以结合使用

相关文章

网友评论

      本文标题:给控件添加边框

      本文链接:https://www.haomeiwen.com/subject/gwsblftx.html