Skip to content

Instantly share code, notes, and snippets.

@humblehacker
Created October 15, 2024 20:55
Show Gist options
  • Save humblehacker/91b58578763717b741bcbe904db1a207 to your computer and use it in GitHub Desktop.
Save humblehacker/91b58578763717b741bcbe904db1a207 to your computer and use it in GitHub Desktop.
Experimenting with AttributedString.transformingAttributes
import SwiftUI
@main
struct SampleApp: App {
var body: some Scene {
WindowGroup {
ContentView()
}
}
}
struct ContentView: View {
var text: AttributedString {
let text = try? AttributedString(markdown: "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut [anim id est laborum](https://dummy).")
.transformingAttributes(\.link) { link in
guard link.value != nil else { return }
let attrs = Text.LineStyle(pattern: .solid, color: Color(uiColor: UIColor.secondaryLabel))
link.replace(with: \.underlineStyle, value: attrs)
}
return text ?? .init()
}
var text2: AttributedString {
let text = try? AttributedString(markdown: "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut [anim id est laborum](https://dummy).")
.transformingAttributes(\.link) { link in
guard link.value != nil else { return }
link.replace(with: \.underlineStyle, value: .single)
}
return text ?? .init()
}
var body: some View {
VStack {
Text(text)
.padding()
Text(text2)
.padding()
}
.font(.caption)
.foregroundColor(Color(uiColor: UIColor.secondaryLabel))
.background(Color(uiColor: UIColor.secondarySystemBackground))
.frame(width: 400, height: 400) }
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
@humblehacker
Copy link
Author

transformingAttributes

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment