Using NSAttributedString with Text in SwiftUI?

Is there a way to format text within a Text view? I couldn't figure out how to use NSAttributedString as input for Text. I have Catalina Beta 6.

Answered by Vincou in 378477022

Instead of using the

HStack {
Text("Hello")
Text("World").italic()
}

Solution proposed above, you should use the + operator between Text() as mentioned in the link below

https://www.hackingwithswift.com/quick-start/swiftui/how-to-combine-text-views-together


Text("Hello")
+
Text("World")
.italic()

You should write like:

Text("Hello World")

.font(.largeTitle)

.foregroundColor(Color.green)

Asking my question another way, how do I format some of the characters within a Text view?


Hello World!

I would write 2 Text in sequence.

Yes, for that trivial example I can make

HStack { 
  Text("Hello")
  Text("World").italic()
}


But that doesn't go very far if I want fully rich text formatting. Here's a tougher example that multiple Text doesn't handle:

You can manually conform to
ObservableObject
by defining an objectWillChange publisher that emits before the object changes. However, by default,
ObservableObject
automatically synthesizes objectWillChange and emits before any
@
Published
properties change.

Now if there were a Flow layout it might work to string a lot of Texts together as you suggest.

Accepted Answer

Instead of using the

HStack {
Text("Hello")
Text("World").italic()
}

Solution proposed above, you should use the + operator between Text() as mentioned in the link below

https://www.hackingwithswift.com/quick-start/swiftui/how-to-combine-text-views-together


Text("Hello")
+
Text("World")
.italic()

I fear that for the time being it will be tricky (if possible) to get all the flexibility of NSAttributedString.


What is the "tougher example that multiple Text doesn't handle:"


My personal opinion is that it is better to wait some time and future releases to use SwiftUI for more than basic UI.

Hi All.

In case of an attributed string like


"Normal Bold Italic BoldItalic Underscored UnderscoredBold UnderscoredItalic UnderscoredBoldItalic Normal Bold Italic BoldItalic Underscored UnderscoredBold UnderscoredItalic UnderscoredBoldItalic Normal Bold Italic BoldItalic Underscored UnderscoredBold UnderscoredItalic UnderscoredBoldItalic Normal Bold Italic BoldItalic Underscored UnderscoredBold UnderscoredItalic UnderscoredBoldItalic"


that use more than a line to fit the safe area of the device screen, you can concatenate with "+" the different strings (and correctly attribute them) but all are in a horizontal stack so the results is... 😕

Works like a charm for me. Thanks for the tip!


struct ContentView: View {
    var body: some View {
        Text("This is a test. ")
            + Text("This is a test. ")
            + Text("This is a test. ").foregroundColor(.red)
            + Text("This is a test. ").foregroundColor(.blue)
            + Text("This is a test. ").foregroundColor(.green)
            + Text("This is a test. ").italic()
            + Text("This is a test. ").font(.headline)
            + Text("This is a test. ").bold()
            + Text("This is a test. ").font(.system(size: 12))
            + Text("This was just a test. ")
    }
}

This solution is OK for simple static content, localisation is a problem but it is a problem with NSAttributedString as well.


The App I am developing allows the user to write rich notes to attach to their content, which gets stored in CoreData. I do not see how this solution could work in these circumstances.


Currently we store the rich text as NSAttributedString.


Will NSAttributedString have a future in a SwiftUI world?

Using NSAttributedString with Text in SwiftUI?
 
 
Q