SwiftUI Shadow Tutorial

In SwiftUI a shadow can be applied to any view using the shadow modifier. In this tutorial a text view with a shadow will be displayed . SwiftUI requires Xcode 11 and MacOS Catalina, for which the Betas can be downloaded at the Apple developer portal.

Open Xcode and either click Create a new Xcode project in Xcode’s startup window, or choose File > New > Project. In the template selector, select iOS as the platform, select the Single View App template, and then click Next. Enter SwiftUIShadowTutorial as the Product Name, select the Use SwiftUI checkbox, and click Next. Choose a location to save the project on your Mac.

In the canvas, click Resume to display the preview. If the canvas isn’t visible, select Editor > Editor and Canvas to show it.

In the Project navigator, click to select ContentView.swift. Change the code inside the ContentView struct to

struct ContentView: View {
    var body: some View {
        Text("Shadow in SwiftUI")
            .foregroundColor(Color.red)
            .font(.title)
            .bold()
            .padding()
           .shadow(color: Color.gray, radius: 3, x: 15, y: 20)
    }
}

A text view is displayed with a red font color and in bold. with a shadow color of gray. The shadow is created with the following parameters

color:

The shadow’s color.

radius:

The shadow’s size.

x:

A horizontal offset you use to position the shadow relative to this view.

y:

A vertical offset you use to position the shadow relative to this view.

The preview will look like this.

The source code of the SwiftUIShadowTutorial can be downloaded at the ioscreator repository on Github.