我在 SwiftUI 中有一个文本视图,它将有多行.我知道我可以使用
更改文本的背景颜色Text("").background(Color.red)但是,如果我的文本是多行,则背景颜色将用于文本视图的整个框架,而不仅仅是具有实际文本的部分.像这样:
我只希望实际文本具有背景颜色.这可能吗?
解决方案SwiftUI 似乎还不支持(Xcode 11.3).不过,您可以使用 attributedString 为 UILabel 创建一个 UIViewRepresentable.或者使用 AttributedText> SwiftUIX
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?
解决方案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.
更多推荐
SwiftUI 多行文本背景颜色
发布评论