{"id":35042,"date":"2022-02-16T15:00:49","date_gmt":"2022-02-16T20:00:49","guid":{"rendered":"https:\/\/mjtsai.com\/blog\/?p=35042"},"modified":"2022-02-16T19:58:38","modified_gmt":"2022-02-17T00:58:38","slug":"surprises-when-using-markdown-in-swiftui","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2022\/02\/16\/surprises-when-using-markdown-in-swiftui\/","title":{"rendered":"Surprises When Using Markdown in SwiftUI"},"content":{"rendered":"<p><a href=\"https:\/\/blog.eidinger.info\/3-surprises-when-using-markdown-in-swiftui\">Marco Eidinger<\/a> (via <a href=\"https:\/\/iosdevweekly.com\/issues\/545\">Dave Verwer<\/a>):<\/p>\n<blockquote cite=\"https:\/\/blog.eidinger.info\/3-surprises-when-using-markdown-in-swiftui\"><p>A nice surprise is that SwiftUI supports <a href=\"https:\/\/github.github.com\/gfm\/\">GitHub Flavored Markdown<\/a> (GFM). [&#8230;] An Apple engineer confirmed the use of GFM in the <a href=\"https:\/\/developer.apple.com\/forums\/thread\/682711\">Apple Developer forum<\/a>.<\/p><p>[&#8230;]<\/p><p>Using a string literal in <code>Text<\/code> works. Fine, but using a string variable does not! [&#8230;] The trick is to use <a href=\"https:\/\/developer.apple.com\/documentation\/swiftui\/localizedstringkey\/init(_:)\">init(_:)<\/a> of <code>LocalizedStringKey<\/code>.<\/p><p>[&#8230;]<\/p><p>You might run into the situation that you have an <code>AttributedString<\/code> with line breaks (<code>\\n<\/code>), but those line breaks are not displayed.<\/p><p>The trick is to add the <code>.inlineOnlyPreservingWhitespace<\/code> option when initializing the <code>AttributedString<\/code>.<\/p><\/blockquote>","protected":false},"excerpt":{"rendered":"<p>Marco Eidinger (via Dave Verwer): A nice surprise is that SwiftUI supports GitHub Flavored Markdown (GFM). [&#8230;] An Apple engineer confirmed the use of GFM in the Apple Developer forum.[&#8230;]Using a string literal in Text works. Fine, but using a string variable does not! [&#8230;] The trick is to use init(_:) of LocalizedStringKey.[&#8230;]You might run [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"apple_news_api_created_at":"2022-02-16T20:00:52Z","apple_news_api_id":"3dae25a0-3d3f-42d0-8ffc-ce3d830988f1","apple_news_api_modified_at":"2022-02-17T00:58:41Z","apple_news_api_revision":"AAAAAAAAAAAAAAAAAAAAAg==","apple_news_api_share_url":"https:\/\/apple.news\/APa4loD0_QtCP_M49gwmI8Q","apple_news_coverimage":0,"apple_news_coverimage_caption":"","apple_news_is_hidden":false,"apple_news_is_paid":false,"apple_news_is_preview":false,"apple_news_is_sponsored":false,"apple_news_maturity_rating":"","apple_news_metadata":"\"\"","apple_news_pullquote":"","apple_news_pullquote_position":"","apple_news_slug":"","apple_news_sections":"\"\"","apple_news_suppress_video_url":false,"apple_news_use_image_component":false,"footnotes":""},"categories":[4],"tags":[524,31,2078,30,2077,337,71,901,1812],"class_list":["post-35042","post","type-post","status-publish","format-standard","hentry","category-programming-category","tag-github","tag-ios","tag-ios-15","tag-mac","tag-macos-12","tag-markdown","tag-programming","tag-swift-programming-language","tag-swiftui"],"apple_news_notices":[],"_links":{"self":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/35042","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/comments?post=35042"}],"version-history":[{"count":4,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/35042\/revisions"}],"predecessor-version":[{"id":35048,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/35042\/revisions\/35048"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=35042"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=35042"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=35042"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}