Ios 在SwiftUI中的形状上添加多个文本覆盖
我正在做一个项目,我在SwiftUI中使用不同的形状,例如,这个圆角矩形我想通过叠加在上面添加文本,但它不允许我添加多行。我试图搜索一些覆盖的教程,但总是找到一个只显示一个文本生命的例子 所以我想知道是否有一种方法可以在这样的形状上添加多行文本覆盖,或者是否有更好的方法来实现它。 谢谢你的反馈 以下是当前代码:Ios 在SwiftUI中的形状上添加多个文本覆盖,ios,swiftui,overlay,shapes,Ios,Swiftui,Overlay,Shapes,我正在做一个项目,我在SwiftUI中使用不同的形状,例如,这个圆角矩形我想通过叠加在上面添加文本,但它不允许我添加多行。我试图搜索一些覆盖的教程,但总是找到一个只显示一个文本生命的例子 所以我想知道是否有一种方法可以在这样的形状上添加多行文本覆盖,或者是否有更好的方法来实现它。 谢谢你的反馈 以下是当前代码: import SwiftUI struct ContentView: View { var body: some View { HStack
import SwiftUI
struct ContentView: View {
var body: some View {
HStack {
RoundedRectangle(cornerRadius: 20)
.frame(width: 320, height: 200)
.foregroundColor(Color.blue)
.overlay(
Text("Hello there")
)
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
这是我理想中想要的,但这种方式不行:
import SwiftUI
struct ContentView: View {
var body: some View {
HStack {
RoundedRectangle(cornerRadius: 20)
.frame(width: 320, height: 200)
.foregroundColor(Color.blue)
.overlay(
Text("Hello there")
Text("Hello there")
Text("Hello there")
Text("Hello there")
Text("Hello there")
Text("Hello there")
)
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
将文本与
组
、VStack
、HStack
等任意一种包装在一个容器中
struct ContentView: View {
var body: some View {
HStack {
RoundedRectangle(cornerRadius: 20)
.frame(width: 320, height: 200)
.foregroundColor(Color.blue)
.overlay(
Group{ //<-- Here
Text("Hello there")
Text("Hello there")
Text("Hello there")
Text("Hello there")
Text("Hello there")
Text("Hello there")
}
)
}
}
}
struct ContentView: View {
var body: some View {
HStack {
RoundedRectangle(cornerRadius: 20)
.frame(width: 320, height: 200)
.foregroundColor(Color.blue)
.overlay(
overlayView
)
}
}
// Make your view here
@ViewBuilder
private var overlayView: some View {
Text("Hello there")
Text("Hello there")
Text("Hello there")
Text("Hello there")
Text("Hello there")
Text("Hello there")
}
}