我有两个仅接受数字的文本字段。每当文本在一个字段中更改时,也会同时更新另一个字段。如果清除编辑文本字段,占位符的字体大小将缩小。使用observedObject来跟踪文本的更改和在另一个文本字段中的更新。
VStack{
HStack{
VStack{
Text("FROM")
.kerning(5.0)
.foregroundColor(Color.white.opacity(0.6))
.font(Font.system(size: self.hSize == .compact ? 24 : 48))
.bold()
TextField("amount", text: self.$textBindingManagerFROM.text
, onEditingChanged: {(val) in
self.textBindingManagerFROM.editing = true
self.textBindingManagerTO.editing = false
})
.keyboardType(.decimalPad)
.multilineTextAlignment(.center)
.allowsTightening(true)
.lineLimit(1)
.minimumScaleFactor(0.3)
.font(Font.system(size: self.hSize == .compact ? 24 : 48))
.frame(minHeight: self.hSize == .compact ? 50 : 100, alignment: .leading)
}
VStack{
Text("TO")
.foregroundColor(Color.white.opacity(0.5))
.font(Font.system(size: self.hSize == .compact ? 24 : 48))
.bold()
.kerning(5.0)
TextField("amount", text: self.$textBindingManagerTO.text, onEditingChanged: {(val) in
self.textBindingManagerFROM.editing = false
self.textBindingManagerTO.editing = true
})
.keyboardType(.numberPad)
.multilineTextAlignment(.center)
.allowsTightening(true)
.lineLimit(1)
.minimumScaleFactor(0.3)
.font(Font.system(size: self.hSize == .compact ? 24 : 48))
}
}
.padding(.horizontal, 16)
}
@ObservedObject var textBindingManagerFROM = TextBindingManager(limit: 12, _editing: false)
@ObservedObject var textBindingManagerTO = TextBindingManager(limit: 16, _editing: false)