Using AttributedString
try? it out
import SwiftUI
struct ContentView: View {
let headline = "Headline"
let subheadline = "Subheadline"
let cta = "Read more"
var combinedSubheadline: AttributedString {
var attrString = AttributedString(subheadline + " " + cta)
if let range = attrString.range(of: cta) {
attrString[range].underlineStyle = .single
attrString[range].font = UIFont.preferredFont(forTextStyle: .headline)
}
return attrString
}
var body: some View {
VStack(alignment: .center, spacing: 10) {
Text(headline)
Text(combinedSubheadline)
}
.multilineTextAlignment(.center)
.padding(.horizontal, 20)
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
Supporting Markdown in AttributedString
try? it out
import SwiftUI
struct ContentView: View {
var body: some View {
VStack(alignment: .center, spacing: 20) {
// 1
// bold
Text("**Name**: Alex Paul")
Text("**Occupation**: Software Engineer")
// 2
// italic and links
Text("_Learn more about me_ [here](https://www.alexpaul.dev)")
// 3
// strikethrough
Text("~~copyright 2022~~")
.font(.footnote)
Text("copyright 2023")
.font(.footnote)
}
.padding(40)
.border(.gray, width: 2)
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}