Dashed line border around UIView

后端 未结 23 2324
粉色の甜心
粉色の甜心 2020-12-02 04:16

How do I add dashed line border around UIView.

Something Like this

\"\"

相关标签:
23条回答
  • 2020-12-02 04:29

    In swift 4 I created an UIView extension with the following function:

    func borderDash(withRadius cornerRadius: Float, borderWidth: Float, borderColor: UIColor, dashSize: Int) {
        let currentFrame = self.bounds
        let shapeLayer = CAShapeLayer()
        let path = CGMutablePath()
        let radius = CGFloat(cornerRadius)
    
        // Points - Eight points that define the round border. Each border is defined by two points.
        let topLeftPoint = CGPoint(x: radius, y: 0)
        let topRightPoint = CGPoint(x: currentFrame.size.width - radius, y: 0)
        let middleRightTopPoint = CGPoint(x: currentFrame.size.width, y: radius)
        let middleRightBottomPoint = CGPoint(x: currentFrame.size.width, y: currentFrame.size.height - radius)
        let bottomRightPoint = CGPoint(x: currentFrame.size.width - radius, y: currentFrame.size.height)
        let bottomLeftPoint = CGPoint(x: radius, y: currentFrame.size.height)
        let middleLeftBottomPoint = CGPoint(x: 0, y: currentFrame.size.height - radius)
        let middleLeftTopPoint = CGPoint(x: 0, y: radius)
    
        // Points - Four points that are the center of the corners borders.
        let cornerTopRightCenter = CGPoint(x: currentFrame.size.width - radius, y: radius)
        let cornerBottomRightCenter = CGPoint(x: currentFrame.size.width - radius, y: currentFrame.size.height - radius)
        let cornerBottomLeftCenter = CGPoint(x: radius, y: currentFrame.size.height - radius)
        let cornerTopLeftCenter = CGPoint(x: radius, y: radius)
    
        // Angles - The corner radius angles.
        let topRightStartAngle = CGFloat(Double.pi * 3 / 2)
        let topRightEndAngle = CGFloat(0)
        let bottomRightStartAngle = CGFloat(0)
        let bottmRightEndAngle = CGFloat(Double.pi / 2)
        let bottomLeftStartAngle = CGFloat(Double.pi / 2)
        let bottomLeftEndAngle = CGFloat(Double.pi)
        let topLeftStartAngle = CGFloat(Double.pi)
        let topLeftEndAngle = CGFloat(Double.pi * 3 / 2)
    
        // Drawing a border around a view.
        path.move(to: topLeftPoint)
        path.addLine(to: topRightPoint)
        path.addArc(center: cornerTopRightCenter,
                    radius: radius,
                    startAngle: topRightStartAngle,
                    endAngle: topRightEndAngle,
                    clockwise: false)
        path.addLine(to: middleRightBottomPoint)
        path.addArc(center: cornerBottomRightCenter,
                    radius: radius,
                    startAngle: bottomRightStartAngle,
                    endAngle: bottmRightEndAngle,
                    clockwise: false)
        path.addLine(to: bottomLeftPoint)
        path.addArc(center: cornerBottomLeftCenter,
                    radius: radius,
                    startAngle: bottomLeftStartAngle,
                    endAngle: bottomLeftEndAngle,
                    clockwise: false)
        path.addLine(to: middleLeftTopPoint)
        path.addArc(center: cornerTopLeftCenter,
                    radius: radius,
                    startAngle: topLeftStartAngle,
                    endAngle: topLeftEndAngle,
                    clockwise: false)
    
        // Path is set as the shapeLayer object's path.
        shapeLayer.path = path;
        shapeLayer.backgroundColor = UIColor.clear.cgColor
        shapeLayer.frame = currentFrame
        shapeLayer.masksToBounds = false
        shapeLayer.setValue(0, forKey: "isCircle")
        shapeLayer.fillColor = UIColor.clear.cgColor
        shapeLayer.strokeColor = borderColor.cgColor
        shapeLayer.lineWidth = CGFloat(borderWidth)
        shapeLayer.lineDashPattern = [NSNumber(value: dashSize), NSNumber(value: dashSize)]
        shapeLayer.lineCap = kCALineCapRound
    
        self.layer.addSublayer(shapeLayer)
        self.layer.cornerRadius = radius;
    }
    
    0 讨论(0)
  • 2020-12-02 04:29

    Swift 5+

    import UIKit
    
    class DashedBorderView: UIView {
    
        private let borderLayer = CAShapeLayer()
    
        init(color: UIColor, width: CGFloat = 1) {
            super.init(frame: CGRect.zero)
    
            let pattern: [NSNumber] = [NSNumber(value: Float(5 * width)), NSNumber(value: Float(3 * width))]
    
            borderLayer.backgroundColor = nil
            borderLayer.fillColor = nil
            borderLayer.lineDashPattern = pattern
            borderLayer.lineWidth = width
            borderLayer.strokeColor = color.cgColor
    
            layer.addSublayer(borderLayer)
        }
    
        required init?(coder: NSCoder) {
            fatalError("init(coder:) has not been implemented")
        }
    
        override func draw(_ rect: CGRect) {
            borderLayer.frame = bounds
            borderLayer.path = UIBezierPath(roundedRect: rect, cornerRadius: layer.cornerRadius).cgPath
        }
    }
    

    How to use:

    // f.e. inside UIViewController
    
    let viewWithDashedBorder = DashedBorderView(color: .red, width: 2)
    view.addSubview(viewWithDashedBorder)
    
    0 讨论(0)
  • 2020-12-02 04:30

    This is if you wanted it in Swift 2

    func addDashedLineBorderWithColor(color:UIColor) {
        let _ = self.sublayers?.filter({$0.name == "DashedBorder"}).map({$0.removeFromSuperlayer()})
        let  border = CAShapeLayer();
        border.name = "DashedBorder"
        border.strokeColor = color.CGColor;
        border.fillColor = nil;
        border.lineDashPattern = [4, 4];
        border.path = UIBezierPath(rect: self.bounds).CGPath
        border.frame = self.bounds;
        self.addSublayer(border);
    
    }
    
    0 讨论(0)
  • 2020-12-02 04:33

    For those of you working in Swift, this class extension on UIView makes it easy. This was based on sunshineDev's answer.

    extension UIView {
      func addDashedBorder() {
        let color = UIColor.red.cgColor
    
        let shapeLayer:CAShapeLayer = CAShapeLayer()
        let frameSize = self.frame.size
        let shapeRect = CGRect(x: 0, y: 0, width: frameSize.width, height: frameSize.height)
    
        shapeLayer.bounds = shapeRect
        shapeLayer.position = CGPoint(x: frameSize.width/2, y: frameSize.height/2)
        shapeLayer.fillColor = UIColor.clear.cgColor
        shapeLayer.strokeColor = color
        shapeLayer.lineWidth = 2
        shapeLayer.lineJoin = CAShapeLayerLineJoin.round
        shapeLayer.lineDashPattern = [6,3]
        shapeLayer.path = UIBezierPath(roundedRect: shapeRect, cornerRadius: 5).cgPath
    
        self.layer.addSublayer(shapeLayer)
        }
    }
    

    To use it:

    anyView.addDashedBorder()
    
    0 讨论(0)
  • 2020-12-02 04:33

    For this you need add CAShapeLayer for that particular object

     CAShapeLayer * dotborder = [CAShapeLayer layer];
        dotborder.strokeColor = [UIColor redColor].CGColor;//your own color
        dotborder.fillColor = nil;
        dotborder.lineDashPattern = @[@4, @2];//your own patten 
        [codeBtn.layer addSublayer:dotborder];
        dotborder.path = [UIBezierPath bezierPathWithRect:codeBtn.bounds].CGPath;
        dotborder.frame = codeBtn.bounds;
    
    0 讨论(0)
  • 2020-12-02 04:37

    • Swift 5

    • Works with autolayout

    • Works with the corner radius

    import UIKit
    
        class DashedBorderView: UIView {
    
        private let dashedLineColor = UIColor.black.cgColor
        private let dashedLinePattern: [NSNumber] = [6, 3]
        private let dashedLineWidth: CGFloat = 4
    
        private let borderLayer = CAShapeLayer()
    
        init() {
            super.init(frame: CGRect.zero)
    
            borderLayer.strokeColor = dashedLineColor
            borderLayer.lineDashPattern = dashedLinePattern
            borderLayer.backgroundColor = UIColor.clear.cgColor
            borderLayer.fillColor = UIColor.clear.cgColor
            borderLayer.lineWidth = dashedLineWidth
            layer.addSublayer(borderLayer)
        }
    
        required init?(coder: NSCoder) {
            fatalError("init(coder:) has not been implemented")
        }
    
        override func draw(_ rect: CGRect) {
            borderLayer.frame = bounds
            borderLayer.path = UIBezierPath(roundedRect: rect, cornerRadius: layer.cornerRadius).cgPath
        }
    }
    
    0 讨论(0)
提交回复
热议问题