Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/frankrausch/attributedstringstyledmarkdown
AttributedString Markdown initializer with custom styling
https://github.com/frankrausch/attributedstringstyledmarkdown
Last synced: about 6 hours ago
JSON representation
AttributedString Markdown initializer with custom styling
- Host: GitHub
- URL: https://github.com/frankrausch/attributedstringstyledmarkdown
- Owner: frankrausch
- License: mit
- Created: 2021-11-18T15:40:46.000Z (almost 3 years ago)
- Default Branch: main
- Last Pushed: 2021-11-18T15:49:04.000Z (almost 3 years ago)
- Last Synced: 2024-08-10T14:17:51.719Z (3 months ago)
- Language: Swift
- Size: 1.08 MB
- Stars: 73
- Watchers: 2
- Forks: 3
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# AttributedString Markdown initializer with custom styling
[`AttributedString`](https://developer.apple.com/documentation/foundation/attributedstring) in iOS 15 and macOS 12 comes with a [Markdown initializer](https://developer.apple.com/documentation/foundation/attributedstring/3796160-init).
----
But:
- There is no styling (not even bold or italic) when [drawing](https://developer.apple.com/documentation/foundation/nsattributedstring/1524971-draw) 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](https://www.urbandictionary.com/define.php?term=You%27re%20Holding%20It%20Wrong):
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*](https://developer.apple.com/documentation/foundation/nspresentationintent) into the `AttributedString` runs.
- There are `inlinePresentationIntent`s, e.g. `.stronglyEmphasized` for bold text.
- There are `presentationIntent`s 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-light.jpg)
![AttributedString Markdown Dark](attributedstring-markdown-dark.jpg)
----
© 2021 [Frank Rausch](https://twitter.com/frankrausch)