Custom fonts with AttributedString and Markdown

What is the best way to customize rendering of Markdown styles, in both UIKit and Swift? For instance, how should I tell it what font to use for normal and Bold text? This is what kept us from using NSAttributedString+HTML for embedded string formatting in the past, and AttributedString+Markdown looks even better for this use case, so I'm hoping the fact that we use a custom font won't be a roadblock.

Is calling .replaceAttributes() the expected pattern, and just doing this for each expected attribute in each string?

Beyond using different Markdown inline or block intents (such as emphasized, code, etc.), you can modify your AttributedString via code after decoding from Markdown to apply additional styling while taking into account the environment of the device (for example, light vs dark mode). To do this you can use the .replaceAttributes(_:with:) or .transformingAttributes(_:_:) functions (along with other mutation functions) to modify the attributes of the AttributedString. For example, to add extra styling to bold ranges, you could add a font attribute to ranges of the inline presentation intent attribute where the intent contains .bold. If you'd like even more control over which ranges to style while still supporting localization and changes from the environment, you can define your own custom attribute key that conforms to MarkdownDecodableAttributedStringKey and use this attribute in your Markdown text to indicate ranges that you should style via code before passing the AttributedString to a UI framework.

Ah -- NSPresentationIntent! That looks very useful to help me identify, say, the bold ranges in order to replace that intent with a custom font. I'll try this once FB9148035 is resolved. This looks much better than the attributes used when NSAttributedString is created via HTML.

It would be even better if I could supply rendering style hints to help the rendered know how to apply various intents, though, since right now I assume UIKit must be using a hardcoded mapping of intent attribute -> styling (font etc).

I made a sample project to show how I solved this:

https://github.com/frankrausch/AttributedStringStyledMarkdown

Custom fonts with AttributedString and Markdown
 
 
Q