Skip to content

Instantly share code, notes, and snippets.

@mbernson
Created April 1, 2022 11:32
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mbernson/1d98bd5eb00c9a8563b6512cb354f256 to your computer and use it in GitHub Desktop.
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.
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
}
}
}
}
@baldursson
Copy link

Exactly what I was looking for. Thank you! 🙏

@inzan
Copy link

inzan commented Oct 9, 2023

Perfect example - thanks!

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