0

I'm pretty new in SwiftUI.

I've created a data with some text and a model file with a String definition. The data file contains some single and multiline text.

descriptionTop: "Beschreibung Oben:"

and

descriptionTop: """
Der Vorstand kann Mitglieder, and some more lines of text ...
"""

I want to format some of the words e.g. in bold, italic, strikethrough etc. I have already tried to add like described in another post.

Text("i want to have multiple **formats** for text in the _same **text box**_ at the same time even if it is really _really_ **really _really_** ~~really~~ long text so it would need to wrap correctly. (and maybe even include [links](https://www.apple.com)!)")

without success. I use an Xcode13 Project and IOS 15.5 deployment Thanks for your support.

Michel
  • 1
  • 1
  • Does this answer your question? [Making parts of text bold in SwiftUI](https://stackoverflow.com/questions/61671313/making-parts-of-text-bold-in-swiftui) – Nirav D Sep 22 '22 at 15:56
  • Check this https://www.hackingwithswift.com/quick-start/swiftui/how-to-render-markdown-content-in-text – Wilson Muñoz Sep 22 '22 at 16:32

1 Answers1

0

With iOS 15+ you can just use AttributedStrings. If you have a static text in your Text View you can use it just like this:

Text("~~A strikethrough example~~")

If you get your string from an variable or from a request this will not work. To solve the problem you can use this method to convert a String to an AttributedString:

func getAttributedString(_ markdown: String) -> AttributedString {
do {
    let attributedString = try AttributedString(markdown: markdown, options: AttributedString.MarkdownParsingOptions(interpretedSyntax: .inlineOnlyPreservingWhitespace))
    return attributedString
} catch {
    print("Couldn't parse: \(error)")
}
return AttributedString("Error parsing markdown")
}

This will convert the String (e.g. from a variable) and convert it to an AttributedString you can use in your Text View like this:

var example = "~~A strikethrough example~~"
Text(getAttributedString(example))
Timo
  • 138
  • 10