iOS 15 SwiftUI AttributedString with Markdown doesn't render line spaces
Originator: | rwgrier | ||
Number: | rdar://FB9190672 | Date Originated: | June 18, 2021, 10:45 AM |
Status: | Open | Resolved: | |
Product: | Foundation | Product Version: | Xcode 13 beta 1 / iOS 15 beta |
Classification: | Incorrect/Unexpected Behavior | Reproducible: | Always |
This is using Xcode 13 beta 1 and iOS 15 beta. If I use an AttributedString initializer with Markdown to create a Text object in SwiftUI, any and all line spaces will be removed from the resulting view. However, if I create a Text object with markdown directly in the string, it will render the line space as expected. In the screenshot, the top (Hello World) line includes the AttributedString. The line breaks are not rendered at all here. The bottom line includes markdown directly in the Text initializer and renders the line break without issue. I've included a simple Xcode project to demonstrate this. Project Github Repo: https://github.com/rwgrier/feedback-FB9190672
Comments
Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!
My response to Apple
Thank you so much for getting back to me. I’ve tried this approach in Xcode 13 beta 2 without any success. I’ve included an updated Sample project. Am I still doing this wrong?
The options I am passing in are this:
AttributedString.MarkdownParsingOptions(interpretedSyntax: .inlineOnlyPreservingWhitespace)
Thank you! Ryan
Response from Apple
The difference in behavior you are experiencing is expected. The
AttributedString(markdown:)
initializer applies semantic attributes to ranges of text, it does not insert any additional characters to “render” the text. According to the CommonMark specification that we are following here, theAttributedString
created with the markdown initializer does not contain newline characters and instead contains separate paragraph attributes denoting the start and end of paragraphs. On the contrary, SwiftUI’sText
view uses theMarkdownParsingOptions.InterpretedSyntax.inlineOnlyPreservingWhitespace
option when parsing text input as markdown, causing all whitespace characters (including newlines) to be preserved in the output. If you were to use the.inlineOnlyPreservingWhitespace
option, the whitespace in theirAttributedString
would be preserved in the same way thatText
preserves whitespace.