nirav
nirav

Reputation: 661

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

Upvotes: 2

Views: 1682

Answers (3)

R.Chauhan
R.Chauhan

Reputation: 51

Use this - 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)

Upvotes: 0

Nikolay Khramchenko
Nikolay Khramchenko

Reputation: 421

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
    }
}

Upvotes: 3

Mohammad Sadiq
Mohammad Sadiq

Reputation: 5241

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)

Upvotes: 0

Related Questions