给带有圆角的 UILabel 添加阴影 swift

Add shadow to UILabel with rounded corners swift

我想实现这样的目标:

我在我的 UILabel 上设置了圆角,如下所示:

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

我试过像这样添加阴影:

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
}

我尝试通过在 UIView 中添加一个 UILabel 来实现

  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