How to add shadow on text swift?


If you’re developing a game or a kids application or an application where you want to make attractive user interface you must know how to add shadow on text. This will not only makes the text attractive but also it will also enhance the user experience.

Here we will see how we can add shadow on text.

Step 1 − Open Xcode → New Project → Single View Application → Let’s name it “ShadowText”

Step 2 − Add label in Main.storyboard and create @IBOutlet of the label and name it lblHelloWorld.

Step 3 − Add the below code in your ViewController.swift, add the complete extension

extension UILabel {
   func UILableTextShadow(color: UIColor){
      self.textColor = color
      self.layer.masksToBounds = false
      self.layer.shadowOffset = CGSize(width: 1, height: 1)
      self.layer.rasterizationScale = UIScreen.main.scale
      self.layer.shadowRadius = 6.0
      self.layer.shadowOpacity = 1.0
   }
}

Now from viewDidLoad call the above function on label as shown below.

override func viewDidLoad() {
   super.viewDidLoad()
   self.lblHelloWorld.UILableTextShadow(color: UIColor.red)
}

Run the application to see the effect.

Sharon Christine
Sharon Christine

An investment in knowledge pays the best interest

Updated on: 30-Jul-2019

541 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements