如何防止在文本字段中输入特定字符? [英] How to prevent specific characters from being typed in textfield?

查看:30
本文介绍了如何防止在文本字段中输入特定字符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何防止在 UITextfield 中输入某些字符.例如,如果我不希望在此文本字段中输入字母 A、G、P、Q、X,但允许输入其余字母.

I would like to know how I could prevent certain characters from being allowed to be typed in a UITextfield. For example If I do not want the letters A,G,P,Q,X from being typed inside this textfield but the rest of the letters are allowed.

我对此很陌生,感谢您的帮助!

I am very new to this and thank you for the help!

推荐答案

为此您需要使用 UITextViewDelegate.在其中一种委托方法中捕获用户的输入,并根据需要进行处理.

You need to use UITextViewDelegate for that. Catch user's input in one of the delegate methods, and handle it as you wish.

这是它的 Apple 参考:

Here is the Apple Reference for it:

https://developer.apple.com/library/ios/documentation/UIKit/Reference/UITextViewDelegate_Protocol/#//apple_ref/occ/intfm/UITextViewDelegate/textView:shouldChangeTextInRange:replacementText:

您可能需要使用此功能.

You might need to use this function.

func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
    inputString = inputString.stringByReplacingCharactersInRange(range, withString: string)

这实际上取决于您想如何处理不允许"特定字符.

It really depends on how you want to handle "not allowing" specific chars.

这篇关于如何防止在文本字段中输入特定字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆