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

时间:2015-07-17 16:32:04

标签: ios xcode swift

我希望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
}

7 个答案:

答案 0 :(得分:9)

一种简单的方式是:

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

答案 1 :(得分:2)

针对仅允许使用空格,大写和退格的人进行更新

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
}

答案 2 :(得分:1)

Swift 4.2代码,如果用户要删除错误的字符,则仅允许使用带有退格键的字母

if ($myString) {
 'set true variable' 
} else { 
 'set false variable' 
}

答案 3 :(得分:0)

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个字符

答案 4 :(得分:0)

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
}

答案 5 :(得分:0)

带空格的快捷键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
}

答案 6 :(得分:0)

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

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
    }

}