12

Is there a way to set the keyboard to a textfield to English language only? I don´t want the user to be able to switch to another language.

Many people think/says it is not possible, but the answer on this topic with 14 up votes, begs to differ. Only problems is, it is written in objective c and I only know swift... iPhone: Change Keyboard language programmatically

Community
  • 1
  • 1
simiboy96
  • 207
  • 2
  • 5
  • 14
  • It is impossible with your native App. You only can change this by go to Setting -> Language. Check this http://stackoverflow.com/questions/12595970/iphone-change-keyboard-language-programmatically – Twitter khuong291 Apr 23 '16 at 17:39
  • 1
    @simiboy96 Your requirement is a bit vague. I can select a Spanish or French keyboard (for example) and still type English words. Or I can type lots of different words in Spanish or French using the US English keyboard. So what do you really wish to prevent the user from doing? Do you only want to allow the user to enter text using the letters A-Z with no accents or other diacritical marks or do you really only want to allow the user to enter words in the English language? – rmaddy Apr 23 '16 at 18:18
  • And what about people that only have one keyboard setup, say for example, Chinese? – rmaddy Apr 23 '16 at 18:18
  • @khuong291 If you look at the answer below the initial answer, the answer with 14 up votes, says it is now possible. Only problem is, it is written in objective c... – simiboy96 Apr 23 '16 at 20:11

5 Answers5

14
textField.keyboardType = .asciiCapable

Should do it.

Moris Kramer
  • 1,490
  • 1
  • 12
  • 13
4

F iOS 10.0 and above:

a) Objective-C

yourTextfield.keyboardType = UIKeyboardTypeASCIICapableNumberPad

b) Swift

yourTextfield.keyboardType = .asciiCapableNumberPad
Shahul Hasan
  • 300
  • 3
  • 2
2
yourTextField.keyboardType = UIKeyboardType.alphabet

this is the solution. :)

  • Please add some explanation to your answer such that others can learn from it – Nico Haase Apr 23 '20 at 15:04
  • @NicoHaase Why do you need more explanation? – Kang Developer May 11 '20 at 07:33
  • What do you mean by "why"? Which parts of "such that others can learn from it" are unclear? Have a look at https://stackoverflow.com/help/how-to-answer for some general hints for good answers – Nico Haase May 11 '20 at 07:49
  • 4
    The Q is "setting keyboard to a textfield to English language only" So, A is "yourTextField.keyboardType = UIKeyboardType.alphabet" what can I say to help this more? Do I explain "alphabet"? – Kang Developer May 12 '20 at 01:42
1

I know it's an old question, but here it is:

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
    if !["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"].contains(string.lowercased()) {
        return false
    }

    return true
}

That's a UITextFieldDelegate method, so you need to make your View Controller conform to it. You also need to set your textfield's delegate to self, like so:

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view.

    myTextField.delegate = self
}

What it does, is to check if each letter the user types belong to the list of letters above... if it doesn't, then it doesn't allow the textfield to change.

Sotiris Kaniras
  • 520
  • 1
  • 12
  • 30
0

Set The Delegate

Someone Answered it in another group and it works for me just don't miss to set "yourTextField.delegate = self" in your viewDidLoad


It Shows Arabic Keyboard but don't worry when you start typing it's converting automatically all arabic digits into English one and you can see it by your self also in the screen when you start typing ✅

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
        if textField.keyboardType == .numberPad && string != "" {
            let numberStr: String = string
            let formatter: NumberFormatter = NumberFormatter()
            formatter.locale = Locale(identifier: "EN")
            if let final = formatter.number(from: numberStr) {
                textField.text =  "\(textField.text ?? "")\(final)"
            }
                return false
        }
        return true
    }