SwiftUI的textfile
记录一下SwiftUI的textfile的基本使用方法
import SwiftUIstruct TextfileBootCamp: View {@State var enterString = ""@State var enterList = [String]()var body: some View {NavigationView(content: {VStack {/// 双向绑定输入的字符TextField(LocalizedStringKey("请输入点什么"), text: $enterString).font(.headline).padding().foregroundColor(.black).background(Color.gray.opacity(0.3).cornerRadius(10))Button {guard !textIsAppropriate() else {return}save()} label: {Text("save".uppercased()).padding().foregroundColor(.white).frame(maxWidth: .infinity).background(textIsAppropriate() ? .gray : .blue).cornerRadius(10)}.disabled(textIsAppropriate())Spacer()/// 遍历数组输出,已输入的文案ForEach(enterList, id: \.self) { string inText(string)}}.padding().navigationTitle("TextfileBootCamp !")})}func textIsAppropriate() -> Bool {if enterString.count < 3 {return true}return false}func save() {enterList.append(enterString)enterString = ""}
}#Preview {TextfileBootCamp()
}
效果如下: