我们可以使用+从几个小的文本视图中创建新的文本视图,这是创建更高级格式的简便方法。例如,这将创建三个不同颜色的文本视图并将它们组合在一起:
struct ContentView: View {
var body: some View {
Text("Colored ")
.foregroundColor(.red)
+
Text("SwifUI ")
.foregroundColor(.green)
+
Text("Text")
.foregroundColor(.blue)
}
}
但是我如何通过循环创建组合文本视图。例如(不起作用)
View v;
ForEach((1...3), id: \.self) {
v.append(Text("\($0)"))
}
(1...3).map { Text("\($0)") }.reduce(Text(""), +)
请注意,Text("")
的作用类似于+
操作的标识元素。