Created
April 1, 2022 11:32
-
-
Save mbernson/1d98bd5eb00c9a8563b6512cb354f256 to your computer and use it in GitHub Desktop.
Customizing the styling attributes of the bold text in a Markdown attributed string in Swift.
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 | |
import Foundation | |
struct MarkdownView: View { | |
var body: some View { | |
Text(try! AttributedString(markdown: "This is a **basic** _string_.", customBoldColor: .green)) | |
.font(.title) | |
} | |
} | |
extension AttributedString { | |
init(markdown: String, customBoldColor: Color) throws { | |
try self.init(markdown: markdown) | |
for run in runs { | |
if run.inlinePresentationIntent == .stronglyEmphasized { | |
self[run.range].foregroundColor = customBoldColor | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Exactly what I was looking for. Thank you! 🙏