Quick Tip Icon
Quick Tip

Add underline and strikethrough styles to Text

Since iOS 15 and macOS 12 we can create SwiftUI Text views with AttributedString.

Within the AttributedString we can assign underlineStyle and strikethroughStyle attributes to particular ranges of string. SwiftUI Text view then renders these styles appropriately.

We can use Text.LineStyle to customize the pattern and the color of the line.

struct ContentView: View {
    var attributedString: AttributedString {
        var result = AttributedString("Hello World!")
        result.underlineStyle = Text.LineStyle(
            pattern: .dash, color: .purple)
        return result
    }
    
    var body: some View {
        Text(attributedString)
    }
}
Swift Gems by Natalia Panferova book coverSwift Gems by Natalia Panferova book cover

Level up your Swift skills!$35

100+ tips to take your Swift code to the next level

Swift Gemsby Natalia Panferova

  • Advanced Swift techniques for experienced developers bypassing basic tutorials
  • Curated, actionable tips ready for immediate integration into any Swift project
  • Strategies to improve code quality, structure, and performance across all platforms

Level up your Swift skills!

100+ tips to take your Swift code to the next level

Swift Gems by Natalia Panferova book coverSwift Gems by Natalia Panferova book cover

Swift Gems

by Natalia Panferova

$35