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:38

    In Swift 3

    let border = CAShapeLayer();
    border.strokeColor = UIColor.black.cgColor;
    border.fillColor = nil;
    border.lineDashPattern = [4, 4];
    border.path = UIBezierPath(rect: theView.bounds).cgPath
    border.frame = theView.bounds;
    theView.layer.addSublayer(border);
    
    0 讨论(0)
  • 2020-12-02 04:39

    Swift 3:

    import UIKit
    
    class UIViewWithDashedLineBorder: UIView {
    
        override func draw(_ rect: CGRect) {
    
            let path = UIBezierPath(roundedRect: rect, cornerRadius: 0)
    
            UIColor.purple.setFill()
            path.fill()
    
            UIColor.orange.setStroke()
            path.lineWidth = 5
    
            let dashPattern : [CGFloat] = [10, 4]
            path.setLineDash(dashPattern, count: 2, phase: 0)
            path.stroke()
        }
    }
    

    Use in a storyboard (as custom class) or directly in code:

    let v = UIViewWithDashedLineBorder(frame: CGRect(x: 0, y: 0, width: 100, height: 100))
    

    Result:

    0 讨论(0)
  • 2020-12-02 04:40

    Swift 4.2

    Based off rmooney's answer as a UIView extension with configurable parameters that have default values set.

    Note this does not work if the view has self.translatesAutoresizingMaskIntoConstraints = false

    extension UIView {
      func addDashedBorder(_ color: UIColor = UIColor.black, withWidth width: CGFloat = 2, cornerRadius: CGFloat = 5, dashPattern: [NSNumber] = [3,6]) {
    
        let shapeLayer = CAShapeLayer()
    
        shapeLayer.bounds = bounds
        shapeLayer.position = CGPoint(x: bounds.width/2, y: bounds.height/2)
        shapeLayer.fillColor = nil
        shapeLayer.strokeColor = color.cgColor
        shapeLayer.lineWidth = width
        shapeLayer.lineJoin = CAShapeLayerLineJoin.round // Updated in swift 4.2
        shapeLayer.lineDashPattern = dashPattern
        shapeLayer.path = UIBezierPath(roundedRect: bounds, cornerRadius: cornerRadius).cgPath
    
        self.layer.addSublayer(shapeLayer)
      }
    }
    
    0 讨论(0)
  • 2020-12-02 04:40

    try bellow code

    - (void)drawRect:(CGRect)rect {
        //// Color Declarations
        UIColor* fillColor = [UIColor colorWithRed: 1 green: 1 blue: 1 alpha: 1];
        UIColor* strokeColor = [UIColor colorWithRed: 0.29 green: 0.565 blue: 0.886 alpha: 1];
    
        //// Rectangle Drawing
        UIBezierPath* rectanglePath = [UIBezierPath bezierPathWithRoundedRect:rect cornerRadius: 6];
        [fillColor setFill];
        [rectanglePath fill];
        [strokeColor setStroke];
        rectanglePath.lineWidth = 1;
        CGFloat rectanglePattern[] = {6, 2, 6, 2};
        [rectanglePath setLineDash: rectanglePattern count: 4 phase: 0];
        [rectanglePath stroke];
        [super drawRect:rect];
    }
    

    for one like bellow

    0 讨论(0)
  • 2020-12-02 04:40

    I ended up creating a IB Designable using some of @Chris implementation:

    CurvedDashedBorderUIVIew.h:

    #import <UIKit/UIKit.h>
    
    IB_DESIGNABLE
    @interface CurvedDashedBorderUIVIew : UIView
    
    @property (nonatomic) IBInspectable CGFloat cornerRadius;
    @property (nonatomic) IBInspectable UIColor *borderColor;
    @property (nonatomic) IBInspectable int dashPaintedSize;
    @property (nonatomic) IBInspectable int dashUnpaintedSize;
    
    @property (strong, nonatomic) CAShapeLayer *border;
    
    @end
    

    CurvedDashedBorderUIVIew.m:

    #import "CurvedDashedBorderUIVIew.h"
    
    @implementation CurvedDashedBorderUIVIew
    
    - (instancetype)init
    {
        self = [super init];
        if (self) {
            [self setup];
        }
        return self;
    }
    
    - (instancetype)initWithCoder:(NSCoder *)coder
    {
        self = [super initWithCoder:coder];
        if (self) {
            [self setup];
        }
        return self;
    }
    
    - (instancetype)initWithFrame:(CGRect)frame
    {
        self = [super initWithFrame:frame];
        if (self) {
            [self setup];
        }
        return self;
    }
    
    -(void)setup
    {
        _border = [CAShapeLayer layer];
        [self.layer addSublayer:_border];
    }
    
    -(void)layoutSubviews {
        [super layoutSubviews];
        self.layer.cornerRadius = self.cornerRadius;
    
        _border.strokeColor = self.borderColor.CGColor;
        _border.fillColor = nil;
        _border.lineDashPattern = @[[NSNumber numberWithInt:_dashPaintedSize],
                                    [NSNumber numberWithInt:_dashUnpaintedSize]];
        _border.path = [UIBezierPath bezierPathWithRoundedRect:self.bounds cornerRadius:self.cornerRadius].CGPath;
        _border.frame = self.bounds;
    }
    
    @end
    

    then just set it up in the xib/storyboard:

    0 讨论(0)
  • 2020-12-02 04:40

    You can simply create a IBDesignable class like this:

    import UIKit
    
    @IBDesignable
    class BorderedView: UIView {
    
        @IBInspectable var cornerRadius: CGFloat = 0
    
        @IBInspectable var borderWidth: CGFloat = 0
    
        @IBInspectable var borderColor: UIColor = UIColor.clear
    
        override func draw(_ rect: CGRect) {
            let path = UIBezierPath(roundedRect: rect, cornerRadius: cornerRadius)
            path.lineWidth = borderWidth
    
            borderColor.setStroke()
    
            let dashPattern : [CGFloat] = [10, 4]
            path.setLineDash(dashPattern, count: 2, phase: 0)
            path.stroke()
        }
    
    }
    

    Then just subclass your view with BorderedView from Xcode. This way you can set the border color and border width very easily from the interface builder!

    0 讨论(0)
提交回复
热议问题