[英]Fill text overlay based on Text size on RoundedRectangle
Is there a native way to fill in a shape and make the width based on the size of the Text overlay?有没有一种本机方法可以填充形状并根据文本覆盖的大小设置宽度?
Here is the code that I have:这是我的代码:
RoundedRectangle(cornerRadius: 25, style: .continuous)
.stroke(Color.black, lineWidth: 1)
.frame(width: 100, height: 20, alignment: Alignment.top)
.overlay(
Text(item.category)
.font(.caption.weight(.semibold))
.foregroundColor(.accentColor)
)
This created the following rounded rectangle:这创建了以下圆角矩形:
Does anyone know how to make the rounded rectangle the size of the Text inside?有谁知道如何使圆角矩形成为里面文本的大小? So basically, instead of passing a width, I want the RoundedRectangle to expand based on the width of the Text automatically, but also leave some padding.
所以基本上,我希望 RoundedRectangle 根据文本的宽度自动扩展,而不是传递宽度,但也保留一些填充。
You can try this你可以试试这个
Text(item.category)
.font(.caption.weight(.semibold))
.foregroundColor(.accentColor)
.padding(10) // your preference
.overlay(
RoundedRectangle(cornerRadius: 25, style: .continuous)
.stroke(.black, lineWidth: 1)
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.