How to Add Strikethrough Text in iOS SwiftUI

In this blog post, we’re exploring a simple yet impactful stylistic aspect of text presentation – implementing strikethrough text in SwiftUI. The strikethrough effect can be useful in various scenarios, such as indicating completed tasks, demonstrating price reductions, or creating unique design elements.

Basics: Create Strikethrough Text

In SwiftUI, creating strikethrough text is as easy as applying a single modifier to your text view: .strikethrough().

Text("Hello, SwiftUI!")

In this example, the text “Hello, SwiftUI!” will be displayed with a line through its middle.

SwiftUI strikethrough text

Strikethrough with Color

By default, the strikethrough line adopts the color of the text. However, SwiftUI allows you to customize the color of the strikethrough line. Here’s how you can do it:

Text("Hello, SwiftUI!")
    .strikethrough(color: .red)

In the above snippet, “Hello, SwiftUI!” will be presented with a red strikethrough line.

Strikethrough text SwiftUI

Incorporating strikethrough text in SwiftUI is an effective way to add meaningful emphasis or distinctive design elements to your app’s user interface. With SwiftUI’s simple yet powerful modifier, creating, customizing, and conditionally applying strikethrough text can be achieved with ease.

Similar Posts

Leave a Reply