1

How can i change the keyboard language in the swiftUI. The uikit property is also ok. I have tried this:

public extension UITextField 
{ 
    // ⚠️ Prefer english keyboards
    //
    override var textInputMode: UITextInputMode?
    {
        let locale = Locale(identifier: "en_US") // your preferred locale

        return
            UITextInputMode.activeInputModes.first(where: { $0.primaryLanguage == locale.languageCode })
            ??
            super.textInputMode
    }
}

But it not work.

ios coder
  • 1
  • 4
  • 31
  • 91

1 Answers1

1

I have observed that UITextInputMode.primaryLanguage uses - instead of _ in locale string. So if you create locales as Locale(identifier: "en-US") instead of Locale(identifier: "en_US"), then UITextInputMode.primaryLanguage and locale.identifier has the same format en-US.

public extension UITextField
{
    override var textInputMode: UITextInputMode?
    {
        let locale = Locale(identifier: "en-US")
        
        return
            UITextInputMode.activeInputModes.first(where: { $0.primaryLanguage == locale.identifier })
            ??
            super.textInputMode
    }
}
SamB
  • 1,560
  • 1
  • 13
  • 19