简体   繁体   中英

Label shadow not working in swift

I tried to give a shadow to label text but it does not shown.

My code

private func drawValueLabel() {

    valueLabel.layer.shadowColor = UIColor.red.cgColor
    valueLabel.layer.shadowOffset = CGSize.init(width: 15.0, height: 15.0)
    valueLabel.layer.shadowRadius = 3.0
    valueLabel.layer.shadowOpacity = 1
    valueLabel.layer.masksToBounds = false
    valueLabel.clipsToBounds = false
    valueLabel.layer.shouldRasterize = true

    valueLabel.drawText(in: self.bounds)

}

Help me to show shadow

Thanks

I'm using this UIView extension for add shadow. With this extension you can add shadow from storyboard.

extension UIView {

    @IBInspectable var shadow: Bool {
        get {
            return layer.shadowOpacity > 0.0
        }
        set {
            if newValue == true {
                self.addShadow()
            }
        }
    }

    func addShadow(shadowColor: CGColor = UIColor.black.cgColor,
                   shadowOffset: CGSize = CGSize(width: 1.0, height: 2.0),
                   shadowOpacity: Float = 0.4,
                   shadowRadius: CGFloat = 3.0) {
        layer.shadowColor = shadowColor
        layer.shadowOffset = shadowOffset
        layer.shadowOpacity = shadowOpacity
        layer.shadowRadius = shadowRadius
    }
}

Your code is working fine for me. Try giving some smaller offset so that you would be able to see it like

label.layer.shadowOffset = CGSize.init(width: 3.0, height: 3.0)

使用此label.layer.shadowColor = UIColor.black.cgColor label.layer.shadowOpacity = 0.5 label.layer.shadowRadius = 2.0 label.layer.shadowOffset = CGSize(width: 2.0, height: 2.0)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM