Home > Blockchain >  Change the keyboard language in SwiftUI
Change the keyboard language in SwiftUI

Time:09-29

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.

CodePudding user response:

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
    }
}
  • Related