How to make shadow effect on shortcut in Swift?

I can’t figure out how to encode the shadow on the label. I have a rating label that changes, so just Photoshop with shadows will not be possible. I need to encode it so that it always has a blurry shadow behind the text at all times. Can someone come up with examples or help?


People who say that this is a duplicate, "duplicate" are the dropping shadows on the UIView, mine is about UILabel. This is not the same.

+23
source share
6 answers

Try it, you can run it right on the playground page:

import UIKit
import PlaygroundSupport

let container = UIView(frame: CGRect(x: 0, y: 0, width: 600, height: 400))

container.backgroundColor = UIColor.lightGray

PlaygroundPage.current.liveView = container

var r = CGRect(x: 40, y: 40, width: 300, height: 60)

let label = UILabel(frame: r)
label.font = UIFont.systemFont(ofSize: 44.0)
label.textColor = .white
label.frame = r
label.text = "Hello Blur"

container.addSubview(label)

label.layer.shadowColor = UIColor.black.cgColor
label.layer.shadowRadius = 3.0
label.layer.shadowOpacity = 1.0
label.layer.shadowOffset = CGSize(width: 4, height: 4)
label.layer.masksToBounds = false

Play with different values ​​for the shadow. Color, Opacity, Radius and Offset.

Result:

enter image description here

+56

UILabel , .

enter image description here

enter image description here

+15

. ViewController.

.
enter image description here

extension UILabel {
    func textDropShadow() {
        self.layer.masksToBounds = false
        self.layer.shadowRadius = 2.0
        self.layer.shadowOpacity = 0.2
        self.layer.shadowOffset = CGSize(width: 1, height: 2)
    }

    static func createCustomLabel() -> UILabel {
        let label = UILabel()
        label.textDropShadow()
        return label
    }
}

myLabel.textDropShadow()
+13

Swift 4 - :

 // Label Shadow
    extension UILabel {
        func lblShadow(color: UIColor , radius: CGFloat, opacity: Float){
            self.textColor = color
            self.layer.masksToBounds = false
            self.layer.shadowRadius = radius
            self.layer.shadowOpacity = opacity

            self.layer.shadowOffset = CGSize(width: 1, height: 1)
            self.layer.shouldRasterize = true
            self.layer.rasterizationScale = UIScreen.main.scale
        }
    }

let titleColor = UIColor(red:0.08, green:0.08, blue:0.08, alpha:1.0)
titleLbl.lblShadow(color: titleColor, radius: 3, opacity: 0.25)
+2

Swift 4, IBInspectable

extension UILabel {

    @IBInspectable var isShadowOnText: Bool {
        get {
            return self.isShadowOnText
        }
        set {
            guard (newValue as? Bool) != nil else {
                return
            }

            if newValue == true{

                self.layer.shadowColor = UIColor.black.cgColor
                self.layer.shadowRadius = 2.0
                self.layer.shadowOpacity = 1.0
                self.layer.shadowOffset = CGSize(width: 2, height: 2)
                self.layer.masksToBounds = false
            }
        }
    }
}
+2

, , .

:

ViewController: UIViewController {

@IBOutlet weak var label: UILabel!
override func viewDidLoad() {
    super.viewDidLoad()

    let shadow = NSShadow()
    shadow.shadowColor = UIColor.blue
    shadow.shadowBlurRadius = 10

    let attrs: [NSAttributedString.Key: Any] = [
        .font: UIFont.systemFont(ofSize: 36),
        .foregroundColor: UIColor.red,
        .shadow: shadow
    ]

    let s = "MY TEXT"
    let attributedText = NSAttributedString(string: s, attributes: attrs)
    self.label.attributedText = attributedText
}

}

:

enter image description here

Note: You must add an assigned string every time, since the shadow is an attribute of the string, not the label, otherwise you can also get the class and override "setText". (storing attributes inside an object in a property that you can set in init / setter)

0
source

All Articles