swift 如何使用 Text 视图创建静态标签?

10 min read

Swift 使用 Text 视图创建静态标签的步骤如下:

  1. 在代码中创建一个 Text 视图。
Text("This is a static label")
  1. 使用 Text 视图的属性来自定义标签。例如,可以设置字体、字体颜色、对齐方式等。
Text("This is a static label")
    .font(.title)
    .foregroundColor(.blue)
    .multilineTextAlignment(.center)
  1. 将 Text 视图添加到视图的层次结构中。
struct ContentView: View {
    var body: some View {
        VStack {
            Text("This is a static label")
                .font(.title)
                .foregroundColor(.blue)
                .multilineTextAlignment(.center)
            // 其他视图
        }
    }
}