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)
}
}


Check out our book!
Integrating SwiftUI into UIKit Apps
Integrating SwiftUI intoUIKit Apps
A detailed guide on gradually adopting SwiftUI in UIKit projects.
- Discover various ways to add SwiftUI views to existing UIKit projects
- Use Xcode previews when designing and building UI
- Update your UIKit apps with iOS 16 features such as Swift Charts and Lock Screen widgets
- Migrate larger parts of your apps to SwiftUI while reusing views and controllers built in UIKit