SwiftUI 多行文本背景颜色

编程入门 行业动态 更新时间:2024-10-09 09:11:56
本文介绍了SwiftUI 多行文本背景颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我在 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 多行文本背景颜色

本文发布于:2023-11-23 00:58:02,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1619538.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:颜色   背景   SwiftUI

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!