Created
October 15, 2024 20:55
-
-
Save humblehacker/91b58578763717b741bcbe904db1a207 to your computer and use it in GitHub Desktop.
Experimenting with AttributedString.transformingAttributes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | |
} | |
} |
Author
humblehacker
commented
Oct 15, 2024
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment