SwiftUI 多行文本背景颜色 [英] SwiftUI Multiline Text Background Color

查看:32
本文介绍了SwiftUI 多行文本背景颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 SwiftUI 中有一个文本视图,它将有多行.我知道我可以使用

更改文本的背景颜色

Text("<长文本>").background(Color.red)

但是,如果我的文本是多行,则背景颜色将用于文本视图的整个框架,而不仅仅是具有实际文本的部分.像这样:

我只希望实际文本具有背景颜色.这可能吗?

解决方案

SwiftUI 似乎还不支持(Xcode 11.3).不过,您可以使用 attributedStringUILabel 创建一个 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 多行文本背景颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆