UITextfield keyboard with only alphabet, no numbers, no caps, no spacebar?

前端 未结 8 1954
深忆病人
深忆病人 2021-02-06 06:33

I want the keyboard for the UITextfield to only have a-z, no numbers, no special characters (!@$!@$@!#), and no caps. Basicly I am going for a keyboard with only the alphabet.

相关标签:
8条回答
  • 2021-02-06 06:53
    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
                }
        }
    

    Try this code
    In above code is only allow 10 char in text field

    0 讨论(0)
  • 2021-02-06 06:57

    An easiet way would be:

    if let range = string.rangeOfCharacterFromSet(NSCharacterSet.letterCharacterSet())
        return true
    }
    else {
        return false
    }
    
    0 讨论(0)
  • 2021-02-06 07:01
    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
    }
    
    0 讨论(0)
  • 2021-02-06 07:04

    Swift 4.2 Code Allow only alphabets with allowing backspace if the user wants to remove wrong character

    func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
        if string.rangeOfCharacter(from: .letters) != nil || string == ""{
            return true
        }else {
            return false
        }
    }
    
    0 讨论(0)
  • 2021-02-06 07:05

    Update for those who wants to Allow Space, Caps & Backspace Only

    Swift 4.x, Swift 5.x & up

    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
    }
    
    0 讨论(0)
  • 2021-02-06 07:09

    Swift 3 solution

    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
    }
    
    0 讨论(0)
提交回复
热议问题