如何为多个文本字段设置 UITextFieldDelegate?

发布于 2025-01-10 19:24:49 字数 1455 浏览 3 评论 0原文

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

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

发布评论

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

评论(1

耳根太软 2025-01-17 19:24:49

如果您的目标是让所有三个文本字段遵循相同的规则,请为这三个文本字段设置委托shouldChangeCharactersIn 只需检查用户当前正在输入的“当前”文本字段。

这是一个小观察,但我也会避免重复重新创建允许字符的 CharacterSet 。您可以简单地将其设为属性。

这将其简化为:

private let allowedCharacters = CharacterSet(charactersIn: "ABCDEFGĞHIİJKLMNOÖPRSŞTUÜVYZ")

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
    if (textField.text?.count ?? 0) - range.length + string.count > 1 {
        return false
    }

    return allowedCharacters.isSuperset(of: CharacterSet(charactersIn: string.localizedUppercase))
}

如果您希望他们只输入大写字符,我会:

  • 将键盘大写更改为“所有字符”,这样他们更有可能不输入小写字母:

    “在此处输入图像描述"

  • 可以选择通过为文本字段添加“编辑已更改”操作来更改大小写至:

    @IBAction func EditingChanged(_ textField: UITextField) {
        textField.text = textField.text?.localizedUppercase
    }
    

    您可能需要尝试是否要使用 localizedUppercaseuppercased()uppercased(with:)

    注意,这个“整个字符串大写”的逻辑有点草率。如果您允许在输入中使用多字符字符串,您确实希望捕获光标所在位置并恢复它。 (否则,这可能是一个烦人的用户体验,如果用户更改多字符字符串的第一个字符,光标将跳到末尾。)例如,一个简单的再现可能是:

    @IBAction func EditingChanged(_ textField: UITextField) {
        让范围 = textField.selectedTextRange
        textField.text = textField.text?.localizedUppercase
        textField.selectedTextRange = 范围
    }
    

    但是对于您的单个字符输入,上面的更简单的示例应该足够了。

If your goal is to have all three text fields follow the same rule, set the delegate for all three. The shouldChangeCharactersIn only needs to check the “current” text field into which the user is currently typing.

A minor observation, but I also would avoid recreating the CharacterSet of allowed characters repeatedly. You can simply make that a property.

That reduces it down to something like:

private let allowedCharacters = CharacterSet(charactersIn: "ABCDEFGĞHIİJKLMNOÖPRSŞTUÜVYZ")

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
    if (textField.text?.count ?? 0) - range.length + string.count > 1 {
        return false
    }

    return allowedCharacters.isSuperset(of: CharacterSet(charactersIn: string.localizedUppercase))
}

If you want them only entering uppercase characters, I would:

  • Change the keyboard capitalization to “All Characters”, so they are more likely to not enter lowercase letters:

    enter image description here

  • Optionally change the capitalization by adding an “editing changed” action for your text field to:

    @IBAction func editingChanged(_ textField: UITextField) {
        textField.text = textField.text?.localizedUppercase
    }
    

    You might have to experiment whether you want to use localizedUppercase or uppercased() or uppercased(with:).

    Note, this “uppercase the whole string” logic is a little sloppy. If you were allowing multi-character strings in your input, you really would want to capture where the cursor was and restore it. (Otherwise it could be an annoying UX where if the user is changing the first character of a multi-character string, the cursor would jump to the end.) E.g., a simple rendition might be:

    @IBAction func editingChanged(_ textField: UITextField) {
        let range = textField.selectedTextRange
        textField.text = textField.text?.localizedUppercase
        textField.selectedTextRange = range
    }
    

    But for your single character input, the simpler example, above, should be sufficient.

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