28 July 2021Add 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)
}
}
Black Friday 2025 offer: 30% off!$35$25
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
Black Friday 2025 offer: 30% off!100+ tips to take your Swift code to the next level
Swift Gems
by Natalia Panferova
$35$25If you are enjoying our blog and would like to support us, you can sponsor us on GitHub.
Subscribe to our monthly newsletter to get exclusive Swift and SwiftUI tips, project updates, behind-the-scenes insights, and special discounts on our books.
For more frequent updates, follow us on X and Bluesky.