Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aheze/87e4fbb09649ab2ad5f6f0c192018911 to your computer and use it in GitHub Desktop.
Save aheze/87e4fbb09649ab2ad5f6f0c192018911 to your computer and use it in GitHub Desktop.
struct ContentView: View {
var body: some View {
Text("Some Attributed String") { string in
string.foregroundColor = .blue
if let range = string.range(of: "Attributed") {
string[range].foregroundColor = .red
}
}
}
}
extension Text {
init(_ string: String, configure: ((inout AttributedString) -> Void)) {
var attributedString = AttributedString(string) /// create an `AttributedString`
configure(&attributedString) /// configure using the closure
self.init(attributedString) /// initialize a `Text`
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment