SwiftUI .strikethrough()

mattogtong's avatar
Published Feb 20, 2023
Contribute to Docs

The .strikethrough view modifier method in SwiftUI can be used to strike through text within a view.

Note: This method is specifically for use with text, so it can be applied as a view modifier to any view that displays text, such as a Text or Label View. However, it cannot be applied to views that do not display text, such as an Image View.

  • Learn how to build iOS applications with Swift and SwiftUI and publish them to Apples' App Store.
    • Includes 7 Courses
    • With Certificate
    • Beginner Friendly.
      13 hours
  • A powerful programming language developed by Apple for iOS, macOS, and more.
    • Beginner Friendly.
      12 hours

Syntax

Text("Some Text")
    .strikethrough(parameters)

This method has two optional parameters:

  • active: A boolean value that determines whether the strikethrough should be applied or not. This parameter defaults to true.
  • color: A color value used to color the strikethrough. This parameter defaults to the color of the text.

Example

The following example demonstrates how to use the .strikethrough() method to apply a blue strikethrough to text:

var body: some View {
Text("Here is a TextView with the .strikethrough() method applied to it using custom parameters.")
.strikethrough(true, color: .blue)
}

This will display:

SwiftUI ViewModifier Strikethrough

All contributors

Contribute to Docs

Learn SwiftUI on Codecademy

  • Learn how to build iOS applications with Swift and SwiftUI and publish them to Apples' App Store.
    • Includes 7 Courses
    • With Certificate
    • Beginner Friendly.
      13 hours
  • A powerful programming language developed by Apple for iOS, macOS, and more.
    • Beginner Friendly.
      12 hours