Search code examples
swiftuilabel

Add shadow to UILabel with rounded corners swift


i want to achieve something like this: Pic

I have set rounded corners on my UILabel like this:

label.layer.cornerRadius = 8.0
label.clipsToBounds = true

and I have tried adding a shadow like this:

func dropShadow(color: UIColor, opacity: Float = 0.5, offSet: CGSize, radius: CGFloat = 1, scale: Bool = true) {
    layer.masksToBounds = false
    layer.shadowColor = color.cgColor
    layer.shadowOpacity = opacity
    layer.shadowOffset = offSet
    layer.shadowRadius = radius

    layer.shadowPath = UIBezierPath(rect: self.bounds).cgPath
    layer.shouldRasterize = true
    layer.rasterizationScale = scale ? UIScreen.main.scale : 1
}

Solution

  • I try to achieve by adding a UILabel inside UIView

      viewlbl.layer.cornerRadius = 20
        viewlbl.clipsToBounds = true
        viewlbl.layer.shadowRadius = 10
        viewlbl.layer.shadowOpacity = 1.0
        viewlbl.layer.shadowOffset = CGSize(width: 3, height: 3)
        viewlbl.layer.shadowColor = UIColor.red.cgColor
        viewlbl.layer.masksToBounds = false
    

    enter image description here