SwiftUI Multiline Text Background Color

天大地大妈咪最大 提交于 2021-02-10 18:27:43

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!