【发布时间】:2022-01-01 12:50:47
【问题描述】:
我通过以下方式在 SwiftUI 中定义了 List 或 Text。
struct SampleView: View {
private let list: [String] = ["Tapping each line should change its color from grey to black", "First Line", "Second Line", "Third Line."]
var body: some View {
ScrollViewReader { value in
List {
ForEach(Array(zip(list, list.indices)), id: \.0) { eachString, index in
Text(eachString)
.bold().font(.largeTitle)
.foregroundColor(.gray)
.onTapGesture {
self.foregroundColor(.black)
}
}
}.listStyle(.plain)
}.frame(maxHeight: .infinity)
}
}
struct SampleView_Preview: PreviewProvider {
static var previews: some View {
SampleView()
}
}
我需要能够在用户点击时更改特定 TextView 的 foregroundColor。
CustomString 是我定义的符合Hashable 的类。我无法让它同时符合ObservableObject,这意味着我无法在onTapGesture 块内更改它的值。
我也无法从循环中删除 index,因为我也需要它。
TIA。
【问题讨论】:
-
请使用tour 并阅读How to Ask 以改进、编辑和格式化您的问题。如果没有Minimal Reproducible Example,就无法帮助您进行故障排除。
标签: ios swift xcode swiftui textview