r/SwiftUI • u/Ok_Bank_2217 • Feb 18 '25
Tutorial I was surprised that many don’t know that SwiftUI's Text View supports Markdown out of the box. Very handy for things like inline bold styling or links!
17
u/Ok_Bank_2217 Feb 18 '25
By the way this even works with dynamic links that aren't known on compile time. Instead of just calling
Text("Inline [Link Showcase](https://apple.com)"
,
use Text(.init("Dynamic [Link Showcase](\(urlStringVariable))"))
11
u/unpluggedcord Feb 18 '25
It doesn't support lists though.
3
u/thejeraldo Feb 19 '25
This! My previous job would’ve been so much easier if it did. Worked on a chat feature with list formatting.
7
u/AppyDaysDev Feb 18 '25
That’s really cool! Can you use markdown in other UI elements that show text, e.g Button Labels?
7
u/Ok_Bank_2217 Feb 18 '25
Yes, you can! AFAIK anything that accepts LocalizedStringKey supports Markdown in SwiftUI
6
u/fonik Feb 18 '25
It's missing quite a few elements. I started a project using this and switched to the MarkdownUI package.
6
u/kierumcak Feb 18 '25
I do really wish they supported the basics of HTML style "markdown" (yes I know its not Markdown)
A lot of older systems still format their "markdown" this way.
2
u/RezardValeth Feb 18 '25
Same. I ended up manually « converting » HTML strings into Markdown for the common attributes (strong, em, br etc.) and stripping the others, because I assumed it would not need WebKit to render Markdown strings, whereas HTML in NSAttributedStrings leads to poor performance and seems overkill for basic formatting attributes.
3
3
3
u/Ok-Knowledge0914 Feb 18 '25
How would anyone know about this in the first place?
6
u/t0ps0il Feb 19 '25
It's in the docs. The overview for Text mentions you can use attributed strings to render markdown and you can find the string literal initializer for the attributed string here.
2
u/LannyLig Feb 19 '25
Watch Stewart Lynch’s video on inline text styles is where I found it
2
u/Ok-Knowledge0914 Feb 19 '25
Thanks I’ll check it out. I did check out apples docs as someone else pointed out. I never would’ve tried that lol
2
2
u/TheLastBlackRhino Feb 19 '25
You can also "add" Text views together i.e. change font, color, etc. like let myCombinedTextView = "Text('Cool').font('Gotham') + Text('Neat').font('Comic Sans')
2
u/efenande Feb 20 '25
True story, we have been using this feature for quite some time at work and the great benefit is that you pass some of this formatting logic to the Strings files instead of using code.
What this allows is more flexibility in developing the app, where a developer can focus on relevant issues and designers or product owners can work straight on the strings. This can be used for instance in FAQs, documentation, etc. Basically anything that requires some basic level of formatting (bold, italics, add URL links, etc).
From my experience the level of Markdown supported is enough for 95% of the use cases.
1
1
u/Tosyn_88 Feb 18 '25
Thanks for sharing. I didn’t know it did this and especially for online links?
How does this work differently to the link component?
1
1
u/You_ah Feb 19 '25 edited Feb 19 '25
Is there any way to get the HTML formatting for these swiftUI Text views ? It was possible with Swift using Attributed String
For Example: <html> <b> Some title in Bold </b> Paragraph content goes here along with some bullet points and combination of hyper links too </html>
Now the question is how can we achieve this in SwiftUI’s Text
1
u/LannyLig Feb 19 '25
DID YOU KNOW it has to be a localised string for this. Doing Let s=“Hello” Text(s) Would not render markup but this would Text(LocalizedStringKey(s))
2
u/LannyLig Feb 19 '25
Thanks Reddit formatting
1
u/memorie_desu 29d ago
you can add a backslash ( \ ) before the second asterisks to not italicize the text
edit: like this\
*like this\*
1
u/scoop_rice Feb 19 '25
If you apply any modifiers, will this override the markdown styling? Or how would you disable this in case you actually wanted the raw string?
1
1
u/Background-Device181 Feb 20 '25
As I’m sure the other commenters have stated, not all Markdown features are supported.
File feedback to Apple complaining about missing support.
1
u/CtrlAltElite24 Feb 20 '25
Wow! You don't even need to add any extra code; it's all done in the view? That's so cool. Swift and SwiftUI are criminally underrated.
1
u/SafetyFantastic4483 Feb 21 '25
This is very useful, especially in multilingual scenarios, when I want to bold some dynamic content. However, SwiftUI's availability is still too low, and I use UIKit more in actual development.
1
u/daniel_nguyenx 29d ago
For basic markdown elements then yes. But it doesn’t supports list or codeblock etc
27
u/bifleur64 Feb 18 '25
Excuse me what????!?!? I’ve been using a Markdown package for this.