3
votes

I have a Text view in SwiftUI that will have multiple lines. I know I can change the background color of my text with

Text("<long text>").background(Color.red)

However if my text is multiple lines the background color will be for the entire frame of the Text view, not just the parts where there is actual text. Like this:

Wrong background

I want only the actual text to have the background color. Is this possible?

1
It seems SwiftUI doesn't support that yet. You can create a UIViewRepresentable for a UILabel with an attributedString though. Or use AttributedText from SwiftUIX. - Yonat
Shame that SwiftUI does not natively support this yet... If you write this as an answer, I'll accept it. - Konstantin Dobler

1 Answers

1
votes

It seems SwiftUI doesn't support that yet (Xcode 11.3). You can create a UIViewRepresentable for a UILabel with an attributedString though. Or use AttributedText from SwiftUIX.