How to display Arabic keyboard in Swift

In this Swift tutorial, we’ll see how we can enable an Arabic keyboard and display an Arabic keyboard for any particular text field.

 

What this blog will cover:

  • custom textfield class
  • Arabic input
  • Arabic keyboard

 

Let’s get started.

Take two simple text fields first in the storyboard. So the initial view looks like this:

 

 

 

Now let’s create a custom textfield class that will inherit from UITextfield. The custom class will be able to use and override all the parent class methods.

 

ArabicTextField class code:

//MARK: - custom textfield class 
class ArabicTextField: UITextField {
    override var textInputMode: UITextInputMode? {
        for inputMode in UITextInputMode.activeInputModes {
            if inputMode.primaryLanguage == "ar" {
                return inputMode
            }
        }
        return super.textInputMode
    }
}

 

So ArabicTextField is a custom subclass of UITextField. and it overrides the textInputMode method of the UItextfield. So the Arabic keyboard will be displayed whenever any text field with this class is active. textInputMode normally returns a normal keyboard, which is an English character keyboard. But we override this functionality and return the specified language type in which we need the input. So it will show the keyboard with the specified language characters.

 

 

Now set the one textfield class in the storyboard to ArabicTextField then create an ArabicTextField outlet, and make the ViewController class its delegate. 

 

Storyboard:

 

 

ViewController:

class ViewController: UIViewController {
    
    @IBOutlet weak var arabic_textfield: ArabicTextField!
   
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // Do any additional setup after loading the view.
        
        arabic_textfield.delegate = self
    }
    
}

 

 

Now let’s run our project and check.

 

 

 

So we can see the Arabic keyboard when the textfield with the ArabicTextField class is active. And when the other textfield with the default textflied class is active, the normal keyboard is presented, which is exactly what we needed.

 

 


 

 

There might be chances that you won’t see any difference. Something like this.

 

 

 

That’s because iOS only allows you to use those language keyboards that have already been added to your phone’s keyboard settings. So to handle this issue, we’ll add an alert if there is no Arabic keyboard already added. The alert will guide the user on how they can add an Arabic keyboard so that the app can access it later.

 

So in the textFieldDidBeginEditing method, the delegate method of the text field. We check the textInputMode of the Arabic textfield. If it’s not equal to Arabic, then we’ll show the alert. Ask the user to add the Arabic keyboard in the settings. After adding the keyboard, it will work as expected.

 

extension ViewController:UITextFieldDelegate{
  
    // UITextFieldDelegate method to detect when editing begins
    func textFieldDidBeginEditing(_ textField: UITextField) {
        
        checkKeyboardLanguage(textField)
        
    }
    
}


//MARK: - handlig arabic fields and keyboardss
extension ViewController{
    
    // Function to check the keyboard language
    func checkKeyboardLanguage(_ textField: UITextField) {
        if let primaryLanguage = textField.textInputMode?.primaryLanguage {
            print("Current keyboard language: \(primaryLanguage)")
            if primaryLanguage != "ar" {
             showArabicKeyboardAlert()
            }
        }
    }
    
    // Function to show an alert if the Arabic keyboard is not enabled
    func showArabicKeyboardAlert() {
        let alert = UIAlertController(
            title: "Enable Arabic Keyboard",
            message: "To type in Arabic, please enable the Arabic keyboard in Settings:\n\nSettings > General > Keyboard > Keyboards > Add Arabic Keyboard...",
            preferredStyle: .alert
        )
        alert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
        present(alert, animated: true, completion: nil)
    }
    
}

 

 

 

 

 

Complete source code:

import UIKit

class ViewController: UIViewController {
    
    @IBOutlet weak var arabic_textfield: ArabicTextField!
   
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // Do any additional setup after loading the view.
        
        arabic_textfield.delegate = self
    }
    
}

extension ViewController:UITextFieldDelegate{
  
    // UITextFieldDelegate method to detect when editing begins
    func textFieldDidBeginEditing(_ textField: UITextField) {
        
        checkKeyboardLanguage(textField)
        
    }
    
}

//MARK: - handlig arabic fields and keyboards
extension ViewController{
    
    // Function to check the keyboard language
    func checkKeyboardLanguage(_ textField: UITextField) {
        if let primaryLanguage = textField.textInputMode?.primaryLanguage {
            print("Current keyboard language: \(primaryLanguage)")
            if primaryLanguage != "ar" {
             showArabicKeyboardAlert()
            }
        }
    }
    
    // Function to show an alert if the Arabic keyboard is not enabled
    func showArabicKeyboardAlert() {
        let alert = UIAlertController(
            title: "Enable Arabic Keyboard",
            message: "To type in Arabic, please enable the Arabic keyboard in Settings:\n\nSettings > General > Keyboard > Keyboards > Add Arabic Keyboard...",
            preferredStyle: .alert
        )
        alert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
        present(alert, animated: true, completion: nil)
    }
    
}


//MARK: - custom textfield class 
class ArabicTextField: UITextField {
    override var textInputMode: UITextInputMode? {
        for inputMode in UITextInputMode.activeInputModes {
            if inputMode.primaryLanguage == "ar" {
                return inputMode
            }
        }
        return super.textInputMode
    }
}


 

Leave a Reply

Your email address will not be published. Required fields are marked *