FREE TRIAL: Accelerate your app development career with Hacking with Swift+! >>

How to render a gradient

Paul Hudson    @twostraws   

Updated for Xcode 13.0

SwiftUI gives us a variety of gradient options, all of which can be used in a variety of ways. For example, you could render a text view using a white to black linear gradient like this:

Text("Hello World")
    .padding()
    .foregroundColor(.white)
    .font(.largeTitle)
    .background(
        LinearGradient(gradient: Gradient(colors: [.white, .black]), startPoint: .top, endPoint: .bottom)
    )

Download this as an Xcode project

The words “Hello World” in white over a gradient fading from white at the top to black at the bottom.

The colors are specified as an array and you can have as many as you want – by default SwiftUI will space them equally. So, we could go from white to red to black like this:

Text("Hello World")
    .padding()
    .foregroundColor(.white)
    .font(.largeTitle)
    .background(
        LinearGradient(gradient: Gradient(colors: [.white, .red, .black]), startPoint: .top, endPoint: .bottom)
    )

Download this as an Xcode project

The words “Hello World” in white over a gradient fading from white at the top to red in the center to black at the bottom.

To make a horizontal gradient rather than a vertical one, use .leading and .trailing for your start and end points:

Text("Hello World")
    .padding()
    .foregroundColor(.white)
    .font(.largeTitle)
    .background(
        LinearGradient(gradient: Gradient(colors: [.white, .red, .black]), startPoint: .leading, endPoint: .trailing)
    )

Download this as an Xcode project

The words “Hello World” in white over a gradient fading from white on the left to red in the center to black on the right.

For alternative gradient styles, try RadialGradient or AngularGradient. As an example, this creates a radial gradient through a variety of colors, starting from the center of the circle and going out to the edges:

Circle()
    .fill(
        RadialGradient(gradient: Gradient(colors: [.red, .yellow, .green, .blue, .purple]), center: .center, startRadius: 50, endRadius: 100)
    )
    .frame(width: 200, height: 200)

Download this as an Xcode project

A circle colored with a gradient transitioning radially outwards from red to yellow to green to blue to purple.

And this creates an angular gradient (often called a conic gradient), cycling through various colors then returning to the beginning:

Circle()
    .fill(
        AngularGradient(gradient: Gradient(colors: [.red, .yellow, .green, .blue, .purple, .red]), center: .center)
    )
    .frame(width: 200, height: 200)

Download this as an Xcode project

A circle colored with a conic gradient transitioning through the colors of the rainbow.

Because all three gradient types conform to the ShapeStyle protocol, you can use them for backgrounds, fills, and strokes. For example, this uses our rainbow conical gradient as a thick inner stroke for a circle:

Circle()
    .strokeBorder(
        AngularGradient(gradient: Gradient(colors: [.red, .yellow, .green, .blue, .purple, .red]), center: .center, startAngle: .zero, endAngle: .degrees(360)),
        lineWidth: 50
    )
    .frame(width: 200, height: 200)

Download this as an Xcode project

A donut shape colored with a conic gradient transitioning through the colors of the rainbow.

Hacking with Swift is sponsored by Sentry

SPONSORED With Sentry’s error and performance monitoring for iOS you see mobile vitals that actually matter, can solve any latency issues quickly, and learn how each release is performing over time.

Get started

Sponsor Hacking with Swift and reach the world's largest Swift community!

Similar solutions…

BUY OUR BOOKS
Buy Pro Swift Buy Swift Design Patterns Buy Testing Swift Buy Hacking with iOS Buy Swift Coding Challenges Buy Swift on Sundays Volume One Buy Server-Side Swift (Vapor Edition) Buy Advanced iOS Volume One Buy Advanced iOS Volume Two Buy Advanced iOS Volume Three Buy Hacking with watchOS Buy Hacking with tvOS Buy Hacking with macOS Buy Dive Into SpriteKit Buy Swift in Sixty Seconds Buy Objective-C for Swift Developers Buy Server-Side Swift (Kitura Edition) Buy Beyond Code

Was this page useful? Let us know!

Average rating: 4.4/5

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.