TextField 即在文本输入期间显示%

发布于 2025-01-17 18:12:16 字数 745 浏览 3 评论 0原文

输入后如何附加%符号


extension ViewController: UITextFieldDelegate {
    
    func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
        let currentText = textField.text! as NSString
        let newText = currentText.replacingCharacters(in: range, with: string)
        if newText.hasSuffix("%"){
            textField.text = newText
        }else{
            textField.text = "\(newText) %"
            
        }
        return true
    }
}

“这是rers”

就像图片中我只想在数字后1%的签名。

how to append % sign after input


extension ViewController: UITextFieldDelegate {
    
    func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
        let currentText = textField.text! as NSString
        let newText = currentText.replacingCharacters(in: range, with: string)
        if newText.hasSuffix("%"){
            textField.text = newText
        }else{
            textField.text = "\(newText) %"
            
        }
        return true
    }
}

This is the rers

as in the picture i just want 1 % sign after the numbers.

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

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

发布评论

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

评论(2

挖鼻大婶 2025-01-24 18:12:16

https://developer.apple.com/documentation/uikit/uitextfielddelegate/1619599-文本字段 说:

返回值: 如果指定的文本范围应该被替换,则为true;
否则,返回 false 以保留旧文本。

可能是因为您返回真正的快速更新,旧文本替换了您刚刚设置的文本字段的文本属性。尝试返回 false,因为这“保留旧文本”,该文本已由您的代码更新。

https://developer.apple.com/documentation/uikit/uitextfielddelegate/1619599-textfield says:

Return Value: true if the specified text range should be replaced;
otherwise, false to keep the old text.

probably because you return true swift updates with the old text replacing the text property of the text-field you just set. Try returning false as this 'keeps the old text' which is already updated by your code.

久夏青 2025-01-24 18:12:16

原因是在文本输入之前呼叫func syrechangecharactersin,这是错误的。我们想要的是在值更改之后,如果没有,则在最后添加%。这是这样做的代码。

// add action for value change recognized
textField.addTarget(self, action: #selector(didChangeValueTextField(textField:)), for: .editingChanged)


@objc final private func didChangeValueTextField(textField: UITextField)
    {
        let text = textField.text ?? ""
        
        if !text.hasSuffix("%") {
            textField.text! = text + "%"
            
            // make cursor position to be left 1 index because final index is %
            let newPosition = textField.position(from: textField.endOfDocument, offset: -1)!
            textField.selectedTextRange = textField.textRange(from: newPosition, to: newPosition)
        }
    }

The reason is func shouldChangeCharactersIn call before the text is input so it is wrong. What we want in here is after the value changed, add % at the end if not have. Here is the code for do that.

// add action for value change recognized
textField.addTarget(self, action: #selector(didChangeValueTextField(textField:)), for: .editingChanged)


@objc final private func didChangeValueTextField(textField: UITextField)
    {
        let text = textField.text ?? ""
        
        if !text.hasSuffix("%") {
            textField.text! = text + "%"
            
            // make cursor position to be left 1 index because final index is %
            let newPosition = textField.position(from: textField.endOfDocument, offset: -1)!
            textField.selectedTextRange = textField.textRange(from: newPosition, to: newPosition)
        }
    }

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