【问题标题】:how to add % symbol at the end textField in swiftUI如何在swiftUI中的文本字段末尾添加%符号
【发布时间】:2022-07-28 13:45:55
【问题描述】:

我想创建一个自定义文本字段来显示金额和 (%) 符号 谁能告诉我我怎么能做到这一点。 如果我输入 12 它应该自动插入 12%

在 UIKit 中它会像 textField.text = "(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) {
    }
}

此代码的问题是它在我开始编写之前显示 % 符号。 我想要的只是当我开始在该字段中写作时,它应该后缀 % 符号

【问题讨论】:

    标签: swiftui textfield


    【解决方案1】:

    对于这种情况,请出示您的代码。因为我不确定这是一个实际的问题,所以也许你在问它之前甚至还没有开始编码。

    你到底在做什么?要显示% 符号,只需在文本中输入(没有格式问题,它不是特殊符号等):

    struct ContentView: View {
        @State var number: Int = 12
        var body: some View {
            Text("$ \(number) %")
                .padding()
        }
    }
    

    你会得到你的视图渲染:

    【讨论】:

    • 我认为 OP 要求 TextField 即在文本输入期间显示 %。
    • 是的,这是我的问题@ChrisR 你对此有什么答案吗
    【解决方案2】:
    TextField("", value: $input, format: .percent )
    

    【讨论】:

      【解决方案3】:

      为此,我创建了一个函数,并在右侧以百分比的形式放置了一个图像,以解决我的问题

          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
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-10-04
        相关资源
        最近更新 更多