我想要做一个自定义的文本字段,它将显示数量和(%)符号,请任何人告诉我如何才能做到这一点。如果我输入12,它应该自动插入12%
在UIKit中,它将类似于textField.text =“(文本) %”
struct UiTextFieldRepresentable: UIViewRepresentable {
@Binding var text: String
func makeUIView(context: Context) -> some UIView {
let textField = UITextField(frame: .zero)
textField.placeholder = "Enter your text"
textField.text = "\(text) %"
return textField
}
func updateUIView(_ uiView: UIViewType, context: Context) {
}
}
这段代码的问题是,在我开始编写之前,它正在显示%签名。我想要的是,当我开始在字段中写作时,它应该在%符号后缀
发布于 2022-07-28 05:45:55
为此,我做了一个函数,在右边放了一个图像,作为解决问题的百分比。
func makeUIView(context: Context) -> UITextField {
let textField = UITextField(frame: .zero)
textField.delegate = context.coordinator
textField.tintColor = UIColor.clear
textField.rightView = makeImageForTextField()
textField.rightViewMode = .always
textField.keyboardType = .decimalPad
let toolBar = UIToolbar(
frame: CGRect(
x: 0, y: 0,
width: textField.frame.size.width,
height: 44
)
)
let doneButton = UIBarButtonItem(
title: "Done",
style: .done,
target: self,
action: #selector(textField.doneButtonTapped(button:))
)
toolBar.items = [doneButton]
toolBar.setItems([doneButton], animated: true)
textField.inputAccessoryView = toolBar
return textField
}
private func makeImageForTextField() -> UIButton {
let button = UIButton()
button.setImage(UIImage(systemName: "percent"), for: .normal)
button.tintColor = .black
button.isUserInteractionEnabled = false
return button
}
发布于 2022-03-29 11:21:43
对于这种情况,请展示您的代码。因为我不确定这是一个实际的问题,也许在问之前你还没有开始编码。
你到底在做什么?要显示%
符号,只需插入文本(没有格式化问题,它不是特殊的符号等等):
struct ContentView: View {
@State var number: Int = 12
var body: some View {
Text("$ \(number) %")
.padding()
}
}
你会得到你的观点:
发布于 2022-03-29 12:22:06
TextField("", value: $input, format: .percent )
https://stackoverflow.com/questions/71660158
复制相似问题