如何在 swiftUI 的末尾文本字段添加 % 符号

发布于 2025-01-17 16:16:58 字数 727 浏览 4 评论 0原文

我想制作一个自定义文本字段,该字段将显示金额和(%)符号 任何人都可以告诉我我如何才能实现这一目标。 如果我输入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) {
    }
}

此代码的问题是在我开始写作之前它显示为%符号。 我想要的是,当我开始在现场写作时,应该将%符号发布

I want to make a custom text field which will display the amount and (%) symbol
can anyone please tell me how can i acheive this.
if i enter 12 it should auto insert 12%

in UIKit it will be like textField.text = "(text) %"

enter image description here

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) {
    }
}

issue with this code is it is showing % sign before i start writing.
all i want want is when i start writing in the field it should postfix the % sign

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

一身仙ぐ女味 2025-01-24 16:16:58
TextField("", value: $input, format: .percent )
TextField("", value: $input, format: .percent )
じее 2025-01-24 16:16:58

为此,我发挥了作用,在右侧将图像放置为修复我的问题的百分比

    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
    }

for this i made a function and on right side put a image as percentage that fixed my issue

    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
    }
遮了一弯 2025-01-24 16:16:58

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

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

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

您将得到呈现的视图:

在此处输入图像描述

For the cases like this, please show your code. Because I'm not sure this is an actual question, maybe you haven't even started coding before asking it.

What exactly are you doing? To display the % symbol, just put in in the text (there're no formatting issues, it's not a special symbol etc):

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

And you'll get your view rendered:

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文