From 923c97cb15c3b5807c10265687ba6b07f5d1307c Mon Sep 17 00:00:00 2001 From: devtorch Date: Sat, 22 Sep 2018 15:57:51 +0300 Subject: [PATCH] feat: add handling to when the user changed the keyboard direction for the textField. --- .../LocalizationHelperDemo/Main.storyboard | 5 +++-- .../LocalizationHelperDemo/ViewController.swift | 15 ++++++++++----- MOLH/MOLH.swift | 14 ++++++++++++++ MOLH/MOLHLanguage.swift | 11 +++++++++++ 4 files changed, 38 insertions(+), 7 deletions(-) diff --git a/LocalizationHelperDemo/LocalizationHelperDemo/Main.storyboard b/LocalizationHelperDemo/LocalizationHelperDemo/Main.storyboard index da64feb..fdfa9c5 100644 --- a/LocalizationHelperDemo/LocalizationHelperDemo/Main.storyboard +++ b/LocalizationHelperDemo/LocalizationHelperDemo/Main.storyboard @@ -1,5 +1,5 @@ - + @@ -84,7 +84,7 @@ - + @@ -120,6 +120,7 @@ + diff --git a/LocalizationHelperDemo/LocalizationHelperDemo/ViewController.swift b/LocalizationHelperDemo/LocalizationHelperDemo/ViewController.swift index e877113..c3633c8 100644 --- a/LocalizationHelperDemo/LocalizationHelperDemo/ViewController.swift +++ b/LocalizationHelperDemo/LocalizationHelperDemo/ViewController.swift @@ -10,16 +10,21 @@ import UIKit class ViewController: UIViewController { @IBOutlet weak var programmaticallylocalizedLabel: UILabel! + @IBOutlet var textField: UITextField! + override func viewDidLoad() { super.viewDidLoad() self.programmaticallylocalizedLabel.text = NSLocalizedString("localize me please", comment: "Localize me Label in the main scene") - // Do any additional setup after loading the view, typically from a nib. +// NotificationCenter.default.addObserver(self, selector: #selector(inputModeDidChange), name: .UITextInputCurrentInputModeDidChange, object: nil) } - override func didReceiveMemoryWarning() { - super.didReceiveMemoryWarning() - // Dispose of any resources that can be recreated. - } +// @objc func inputModeDidChange(_ notification: Notification) { +// if let language = self.textInputMode?.primaryLanguage, MOLHLanguage.isRTLLanguage(language: language) { +// textField.textAlignment = .right +// } else { +// textField.textAlignment = .left +// } +// } @IBAction func switchTheLanguage(_ sender: UIButton) { MOLH.setLanguageTo(MOLHLanguage.currentAppleLanguage() == "en" ? "ar" : "en") diff --git a/MOLH/MOLH.swift b/MOLH/MOLH.swift index d032876..2af75d5 100644 --- a/MOLH/MOLH.swift +++ b/MOLH/MOLH.swift @@ -292,6 +292,20 @@ extension UITextField: TextViewType { } handleSwitching() + listenToKeyboard() + } + + func listenToKeyboard() { + NotificationCenter.default.removeObserver(self, name: .UITextInputCurrentInputModeDidChange, object: nil) + NotificationCenter.default.addObserver(self, selector: #selector(inputModeDidChange), name: .UITextInputCurrentInputModeDidChange, object: nil) + } + + @objc func inputModeDidChange(_ notification: Notification) { + if let language = self.textInputMode?.primaryLanguage, MOLHLanguage.isRTLLanguage(language: language) { + self.textAlignment = .right + } else { + self.textAlignment = .left + } } } diff --git a/MOLH/MOLHLanguage.swift b/MOLH/MOLHLanguage.swift index c42804d..8e92936 100644 --- a/MOLH/MOLHLanguage.swift +++ b/MOLH/MOLHLanguage.swift @@ -108,4 +108,15 @@ open class MOLHLanguage { public static func isRTLLanguage() -> Bool { return currentLocaleIdentifier().hasPrefix("ar") || currentLocaleIdentifier().hasPrefix("fa") } + + /** + Check if the current language is a right to left language + + @param language to be tested + + @return true if its a right to left language + */ + public static func isRTLLanguage(language: String) -> Bool { + return language.hasPrefix("ar") || language.hasPrefix("fa") + } }