Created
June 8, 2021 16:07
-
-
Save aheze/87e4fbb09649ab2ad5f6f0c192018911 to your computer and use it in GitHub Desktop.
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
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