Skip to content

Commit

Permalink
Merge pull request #28 from MoathOthman/26.textfield.issue
Browse files Browse the repository at this point in the history
feat: add handling to when the user changed the keyboard direction fo…
  • Loading branch information
MoathOthman authored Sep 24, 2018
2 parents 1a6c011 + 923c97c commit 0936394
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 7 deletions.
5 changes: 3 additions & 2 deletions LocalizationHelperDemo/LocalizationHelperDemo/Main.storyboard
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="14109" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES" initialViewController="2QC-AV-CKr">
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="14113" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES" initialViewController="2QC-AV-CKr">
<device id="retina4_7" orientation="portrait">
<adaptation id="fullscreen"/>
</device>
Expand Down Expand Up @@ -84,7 +84,7 @@
<action selector="switchTheLanguage:" destination="BYZ-38-t0r" eventType="touchUpInside" id="hnH-TR-2p8"/>
</connections>
</button>
<textField opaque="NO" clipsSubviews="YES" tag="1" contentMode="scaleToFill" contentHorizontalAlignment="left" contentVerticalAlignment="center" borderStyle="roundedRect" placeholder="Centered TextField Case" textAlignment="center" minimumFontSize="17" translatesAutoresizingMaskIntoConstraints="NO" id="fiM-aM-EMW">
<textField opaque="NO" clipsSubviews="YES" tag="-1" contentMode="scaleToFill" contentHorizontalAlignment="left" contentVerticalAlignment="center" borderStyle="roundedRect" placeholder="Centered TextField Case" textAlignment="natural" minimumFontSize="17" translatesAutoresizingMaskIntoConstraints="NO" id="fiM-aM-EMW">
<rect key="frame" x="79" y="306.5" width="217" height="30"/>
<constraints>
<constraint firstAttribute="width" constant="217" id="Lds-iU-Hr3"/>
Expand Down Expand Up @@ -120,6 +120,7 @@
<navigationItem key="navigationItem" title="Main View" id="eV5-Ly-eMc"/>
<connections>
<outlet property="programmaticallylocalizedLabel" destination="qa0-jT-D1A" id="8JT-qW-jzY"/>
<outlet property="textField" destination="fiM-aM-EMW" id="3gA-WO-lYo"/>
</connections>
</viewController>
<placeholder placeholderIdentifier="IBFirstResponder" id="dkx-z0-nzr" sceneMemberID="firstResponder"/>
Expand Down
15 changes: 10 additions & 5 deletions LocalizationHelperDemo/LocalizationHelperDemo/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
14 changes: 14 additions & 0 deletions MOLH/MOLH.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
}

Expand Down
11 changes: 11 additions & 0 deletions MOLH/MOLHLanguage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
}

0 comments on commit 0936394

Please sign in to comment.