总之,我试图在textField中输入一个数值。有一个问题,我正在创建一个自定义文本字段,所以无论我试图为数字键盘添加代码,它都显示错误“调用中的额外参数'格式化程序'”
这是海关的代码TextFiled
import SwiftUI
struct TextFieldCustom: View {
let placeholder: String
@Binding var text: String
@State private var width = CGFloat.zero
@State private var labelWidth = CGFloat.zero
@State private var isEditing = false
public init(placeHolder: String,
text: Binding<String>) {
self._text = text
self.placeholder = placeHolder
}
var body: some View {
TextField(placeholder, text: $text, prompt: Text(placeholder).foregroundColor(.white))
.foregroundColor(.white)
.font(Font.custom("NATS 400", size: 17))
.padding(EdgeInsets(top: 15, leading: 10, bottom: 15, trailing: 10))
.autocapitalization(.none)
.background {
ZStack {
RoundedRectangle(cornerRadius: 5)
.trim(from: 0, to: 0.55)
.stroke(.white, lineWidth: 2)
RoundedRectangle(cornerRadius: 5)
.trim(from: 0.565 + (0.43 * (labelWidth / width)), to: 1)
.stroke(.white, lineWidth: 2)
Text(placeholder)
.font(Font.custom("NATS 400", size: 14))
.foregroundColor(.white)
.overlay( GeometryReader { geo in Color.clear.onAppear { labelWidth = geo.size.width }})
.padding(2)
.font(.caption)
.frame(maxWidth: .infinity,
maxHeight: .infinity,
alignment: .topLeading)
.offset(x: 20, y: -12)
}
}
.overlay( GeometryReader { geo in Color.clear.onAppear { width = geo.size.width }})
.onChange(of: width) { _ in
print("Width: ", width)
}
.onChange(of: labelWidth) { _ in
print("labelWidth: ", labelWidth)
}
}
}
and I am using this on main screen like :-
TextFieldCustom(placeHolder: "Enter Phone Number", text: $phone)
当我把代码如下:-
TextFieldCustom(placeHolder: "Enter Phone Number", text: $phone, formatter: NumberFormatter())
也可以尝试以下代码:-
let formatter: NumberFormatter = {
let formatter = NumberFormatter()
formatter.numberStyle = .decimal
return formatter
}()
文本字段自定义(占位符:“输入电话号码”,文本:$phone,格式化程序:格式化程序)和这个:- .keyboardType(.numberPad),但它们都不工作
所以任何人都可以给予我解决这个问题。谢谢
1条答案
按热度按时间qij5mzcb1#
你能做的就是创建一个字符串扩展来过滤掉数字
在Change Method上,您可以执行以下操作