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

    For Xamarin.iOS dashed/dotted border.

            dottedLayer = new CAShapeLayer();
            dottedLayer.StrokeColor = UIColor.FromRGB(202, 202, 208).CGColor; 
            dottedLayer.FillColor = null;
            dottedLayer.LineDashPattern = new[] { new NSNumber(4), new NSNumber(2) };
    
            dottedLayer.Path = UIBezierPath.FromRect(YourView.Bounds).CGPath; //for square
            dottedLayer.Path = UIBezierPath.FromRoundedRect(YourView.Bounds, 5).CGPath; //for rounded corners
    
            dottedLayer.Frame = YourView.Bounds;
            YourView.Layer.AddSublayer(dottedLayer);
    
    0 讨论(0)
  • 2020-12-02 04:45

    Swift version of the QuartzCore answer.

    import QuartzCore    
    
    let dottedPattern = UIImage(named: "dottedPattern")
    myView.layer.borderWidth = 1
    myView.layer.borderColor = UIColor(patternImage: dottedPattern!).CGColor
    

    The CAShapeLayer approach works, but the QuartzCore approach is better at handling a Table View reload, if the UIView is inside a cell.

    For the image, you can use something like this (it's really small):

    I tend to prefer vector over PNGs when I can get away with it:

    • Within Sketch, create a 4x4 pixel rectangle.
    • Make a total of four of these
    • Group them into a foursquare, alternating colors
    • Export the group as PDF
    • Within Images.xcassets, create a New Image Set called dottedPattern
    • Change the Scale Factors to Single Vector
    • Drop in your PDF
    0 讨论(0)
  • 2020-12-02 04:46

    For Swift 5

    extension UIView {
        func addDashBorder() {
            let color = UIColor.white.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.name = "DashBorder"
            shapeLayer.position = CGPoint(x: frameSize.width/2, y: frameSize.height/2)
            shapeLayer.fillColor = UIColor.clear.cgColor
            shapeLayer.strokeColor = color
            shapeLayer.lineWidth = 1.5
            shapeLayer.lineJoin = .round
            shapeLayer.lineDashPattern = [2,4]
            shapeLayer.path = UIBezierPath(roundedRect: shapeRect, cornerRadius: 10).cgPath
    
            self.layer.masksToBounds = false
    
            self.layer.addSublayer(shapeLayer)
        }
    }
    

    How to add

    vw.addDashBorder()
    

    How to remove border again

    let _ = vw.layer.sublayers?.filter({$0.name == "DashBorder"}).map({$0.removeFromSuperlayer()})
    
    0 讨论(0)
  • 2020-12-02 04:48

    Another method if you like sublayers. In your custom view's init, put this (_border is an ivar):

    _border = [CAShapeLayer layer];
    _border.strokeColor = [UIColor colorWithRed:67/255.0f green:37/255.0f blue:83/255.0f alpha:1].CGColor;
    _border.fillColor = nil;
    _border.lineDashPattern = @[@4, @2];
    [self.layer addSublayer:_border];
    

    And in your layoutsubviews, put this:

    _border.path = [UIBezierPath bezierPathWithRect:self.bounds].CGPath;
    _border.frame = self.bounds;
    
    0 讨论(0)
  • 2020-12-02 04:49
    extension UIView{
    func addDashedLineBorder() {
        let color = UIColor.black.cgColor
    
        let shapeLayer:CAShapeLayer = CAShapeLayer()
        let frameSize = (self.frame.size)
        let shapeRect = CGRect(x: 0, y: 0, width: self.frame.width, height: self.frame.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 = 1
        shapeLayer.lineJoin = kCALineJoinRound
        shapeLayer.lineDashPattern = [2,2]
        shapeLayer.path = UIBezierPath(rect: shapeRect).cgPath
    
        self.layer.addSublayer(shapeLayer)
    }
    

    } and call this function in viewdidLoad() with delay:

    DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) { 
                // Your code with delay
                self.YourView.addDashedBorder()
            }
    
    0 讨论(0)
提交回复
热议问题