问题
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:
I want only the actual text to have the background color. Is this possible?
回答1:
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.
来源:https://stackoverflow.com/questions/59506957/swiftui-multiline-text-background-color