swift UITextfield键盘只有字母,没有数字,没有大写字母,没有空格键?

2hh7jdfx  于 2022-12-02  发布在  Swift
关注(0)|答案(9)|浏览(153)

我希望UITextfield的键盘只有a-z,没有数字,没有特殊字符(!@$!@$@!#),也没有大写字母。基本上,我希望键盘只有字母。
我已经可以禁用空格了。有人知道如何禁用数字、特殊字符和大写吗?任何一个解决方案都会很棒。
是最好的解决方案做下面的所有字符,但我不想?

func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {

    if (string == " ") {
        return false
    }

    if (string == "1") {
        return false
    }

    return true
}
wljmcqd8

wljmcqd81#

最简单的方法是:

if let range = string.rangeOfCharacterFromSet(NSCharacterSet.letterCharacterSet())
    return true
}
else {
    return false
}
ryevplcw

ryevplcw2#

为希望只允许空格、大写和退格的用户提供的更新
Swift 4.x、Swift 5.x及以上版本

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {

    if range.location == 0 && string == " " { // prevent space on first character
        return false
    }

    if textField.text?.last == " " && string == " " { // allowed only single space
        return false
    }

    if string == " " { return true } // now allowing space between name

    if string.rangeOfCharacter(from: CharacterSet.letters.inverted) != nil {
        return false
    }

    return true
}
yrdbyhpb

yrdbyhpb3#

Swift 3解决方案

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {

     let characterSet = CharacterSet.letters

     if string.rangeOfCharacter(from: characterSet.inverted) != nil {
         return false      
     }
     return true
}
9avjhtql

9avjhtql4#

Swift 4.2代码仅允许字母,如果用户希望删 debugging 误字符,则允许退格

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
    if string.rangeOfCharacter(from: .letters) != nil || string == ""{
        return true
    }else {
        return false
    }
}
ej83mcc0

ej83mcc05#

当用户未在文本字段中复制和粘贴时,所有答案均有效。复制和粘贴使用吹塑代码

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {

    let text = (textField.text! as NSString).replacingCharacters(in: range, with: string)
    let Regex = "[a-z A-Z ]+"
    let predicate = NSPredicate.init(format: "SELF MATCHES %@", Regex)
    if predicate.evaluate(with: text) || string == ""
    {
        return true
    }
    else
    {
        return false
    }

}
gajydyqb

gajydyqb6#

if isValidInput(Input: yourtextfieldOutletName.text!) == false {
    let alert = UIAlertController(title: "", message;"Name field accepts only alphabatics", preferredStyle: UIAlertControllerStyle.alert)
    alert.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.default, handler: nil))

    self.present(alert, animated: true, completion: nil)
}

func isValidInput(Input:String) -> Bool {
    let myCharSet=CharacterSet(charactersIn:"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
    let output: String = Input.trimmingCharacters(in: myCharSet.inverted)
    let isValid: Bool = (Input == output)
    print("\(isValid)")

    return isValid
}
cgyqldqp

cgyqldqp7#

class ViewController: UIViewController,UITextFieldDelegate {  

     @IBOutlet var phoneTextField:UITextField!

    override func viewDidLoad() {
            super.viewDidLoad() 
    self.phoneTextField.delegate = self
    }

     func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool
           {
                let textString = (textField.text! as NSString).replacingCharacters(in: range, with: string)

                if textField == self.phoneTextField  && string.characters.count > 0 {
                    let LettersOnly = NSCharacterSet.Letters
                    let strValid = LettersOnly.contains(UnicodeScalar.init(string)!)
                    return strValid && textString.characters.count <= 10
                }
                return true
            }
    }

尝试此代码
在上面的代码中,文本字段只允许10个字符

nsc4cvqm

nsc4cvqm8#

Swift 3带空格

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
    if string == " " { return true }
    if let _ = string.rangeOfCharacter(from: CharacterSet.letters){
        return true
    }
    return false
}
y3bcpkx1

y3bcpkx19#

函数文本字段(_文本字段:UITextField,应更改字符范围:NSRange,替换字符串字符串:字符串)-〉布尔值{让文本=(textField.text!as NSString).范围,其中:字符串)让正则表达式=“[a-z A-Z ]+”让 predicate = NSPredicate.init(格式:“SELF MATCHES %@",正则表达式)if predicate 。evaluate(使用:文字)||字符串==“"{文本字段.文本=文本字段.文本?.替换出现次数(属于:““,与:“”)返回真}否则{返回假}返回真}

相关问题