func addshadow(top: Bool, left: Bool, bottom: Bool, right: Bool, shadowRadius: CGFloat) { self.backgroundColor = UIColor.white self.layer.masksToBounds = false self.layer.cornerRadius = 8 self.layer.shadowOffset = CGSize(width: 0.0, height: 0.0) self.layer.shadowRadius = shadowRadius self.layer.shadowOpacity = 0.4 self.layer.shadowColor = UIColor(red: 6.0/255, green: 141.0/255, blue: 144.0/255, alpha: 1.0).cgColor let path = UIBezierPath() var x: CGFloat = 0 var y: CGFloat = 0 var viewWidth = self.frame.width var viewHeight = self.frame.height // here x, y, viewWidth, and viewHeight can be changed in // order to play around with the shadow paths. if (!top) { y+=(shadowRadius+1) } if (!bottom) { viewHeight-=(shadowRadius+1) } if (!left) { x+=(shadowRadius+1) } if (!right) { viewWidth-=(shadowRadius+1) } // selecting top most point path.move(to: CGPoint(x: x, y: y)) // Move to the Bottom Left Corner, this will cover left edges /* |☐ */ path.addLine(to: CGPoint(x: x, y: viewHeight)) // Move to the Bottom Right Corner, this will cover bottom edge /* ☐ - */ path.addLine(to: CGPoint(x: viewWidth, y: viewHeight)) // Move to the Top Right Corner, this will cover right edge /* ☐| */ path.addLine(to: CGPoint(x: viewWidth, y: y)) // Move back to the initial point, this will cover the top edge /* _ ☐ */ path.close() self.layer.shadowPath = path.cgPath }
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter