AttributedString Markdown initializer with custom styling

Overview

AttributedString Markdown initializer with custom styling

AttributedString in iOS 15 and macOS 12 comes with a Markdown initializer.


But:

  • There is no styling (not even bold or italic) when drawing the AttributedString in a custom view.
  • The AttributedString does have some styling applied when it’s assigned to a UILabel or to a UITextView, but they are noticeably different and weird things happen with the font sizes.
  • All line breaks that should separate the content blocks are missing.
  • The AttributedString colors don’t react to toggling between dark and light mode.

What’s going on?

There is not a lot of information about Markdown in AttributedString out there yet, but I think I was just holding it wrong:

The Markdown initializer merely adds semantic (not visual) markup to the AttributedString. Both UILabel and UITextView interpret these to a certain extent, while drawing a raw (NS)AttributedString doesn’t.

The Markdown initializer merely puts the Markdown parsing results as Presentation Intents into the AttributedString runs.

  • There are inlinePresentationIntents, e.g. .stronglyEmphasized for bold text.
  • There are presentationIntents for blocks like headlines (like .header(1) for a H1).

This sample project

This sample project shows how you can find these intents and replace them with actual AttributedString styling information like font weights and foreground colors.

I don’t know if there’s a more elegant way to solve this. Do let me know if you have a better solution!

AttributedString Markdown Light

AttributedString Markdown Dark


© 2021 Frank Rausch

You might also like...
`resultBuilder` support for `swift-markdown`

SwiftMarkdownBuilder resultBuilder support for swift-markdown. The default way to build Markdown in swift-markdown is to use varargs initializers, e.g

Leverages Apple's Swift-based Markdown parser to output NSAttributedString.
Leverages Apple's Swift-based Markdown parser to output NSAttributedString.

Markdownosaur 🦖 Markdownosaur uses Apple's excellent and relatively new Swift Markdown library to analyze a Markdown source, and then takes that anal

An Objective-C framework for converting Markdown to HTML.

MMMarkdown MMMarkdown is an Objective-C framework for converting Markdown to HTML. It is compatible with OS X 10.7+, iOS 8.0+, tvOS, and watchOS. Unli

Markdown syntax highlighter for iOS
Markdown syntax highlighter for iOS

Marklight Markdown syntax highlighter for iOS and macOS. Description Marklight is a drop in component to easily add realtime Markdown syntax highlight

Rich Markdown editing control for iOS
Rich Markdown editing control for iOS

MarkdownTextView Rich Markdown Editing for iOS MarkdownTextView is an iOS framework for adding rich Markdown editing capabilities. Support for Markdow

Converts Markdown files and strings into NSAttributedStrings with lots of customisation options.
Converts Markdown files and strings into NSAttributedStrings with lots of customisation options.

SwiftyMarkdown 1.0 SwiftyMarkdown converts Markdown files and strings into NSAttributedStrings using sensible defaults and a Swift-style syntax. It us

Markdown parsing and rendering for iOS and OS X
Markdown parsing and rendering for iOS and OS X

CocoaMarkdown Markdown parsing and rendering for iOS and macOS CocoaMarkdown is a cross-platform framework for parsing and rendering Markdown, built o

Blazing fast Markdown / CommonMark rendering in Swift, built upon cmark.
Blazing fast Markdown / CommonMark rendering in Swift, built upon cmark.

Down Blazing fast Markdown (CommonMark) rendering in Swift, built upon cmark v0.29.0. Is your app using it? Let us know! If you're looking for iwasrob

Markdown parser for iOS

Marky Mark Marky Mark is a parser written in Swift that converts markdown into native views. The way it looks it highly customizable and the supported

Owner
Frank Rausch
User Interface Typographer
Frank Rausch
SensibleStyling - Helpers for sensible SwiftUI styling

SensibleStyling Helpers for sensible SwiftUI styling. License See the LICENSE fi

apparata 5 Dec 18, 2022
_Text is a simple augmentation to SwiftUI that makes styling easier

_Text _Text is a simple augmentation to SwiftUI that makes styling easier. By utilizing the environment it is possible to apply text modifiers to any

Eric Lewis 1 Feb 22, 2022
A simple and customizable Markdown Parser for Swift

MarkdownKit MarkdownKit is a customizable and extensible Markdown parser for iOS and macOS. It supports many of the standard Markdown elements through

Bruno Oliveira 687 Dec 18, 2022
MarkdownView is a WKWebView based UI element, and internally use bootstrap, highlight.js, markdown-it.

MarkdownView is a WKWebView based UI element, and internally use bootstrap, highlight.js, markdown-it.

Keita Oouchi 1.8k Dec 21, 2022
Marky Mark is a parser written in Swift that converts markdown into native views.

Marky Mark is a parser written in Swift that converts markdown into native views. The way it looks it highly customizable and the supported markdown syntax is easy to extend.

M2mobi 287 Nov 29, 2022
Notepad - A fully themeable iOS markdown editor with live syntax highlighting.

Notepad is just like any other UITextView, but you need to use the convenience initializer in order to use the themes. To create a new theme, copy one of the existing themes and edit the JSON.

Rudd Fawcett 802 Dec 31, 2022
Markdown in SwiftUI, and some other interesting components.

RoomTime RoomTime is a bundle of tools developed in my app RoomTime Lite. ( ?? RoomTime Lite is still in development) Features TextArea AutoWrap Markd

Chen SiWei 56 Dec 20, 2022
Markdown parser for iOS

Marky Mark Marky Mark is a parser written in Swift that converts markdown into native views. The way it looks it highly customizable and the supported

M2mobi 254 Jun 11, 2021
Swift markdown library

Markdown ![Swift version](https://img.shields.io/badge/Swift-2.1 | 2.2-blue.svg) ![GitHub license](https://img.shields.io/badge/license-LGPL v3-green.

Crossroad Labs 79 Oct 9, 2022
A Pure Swift implementation of the markdown mark-up language

SmarkDown A pure Swift markdown implementation consistent with Gruber's 1.0.1 version. It is released under the BSD license so please feel free to use

Swift Studies 67 Jan 24, 2022