4
votes

I am working on an English / Arabic app that has localization. In one textfield I have set textAlignment property as right. But the content, numeric, displayed at right side.
I double checked it by debugging textAlignment property value and it indeed is right.

I have set alignment property programatically, as it will be changing as per language selection (i.e. on button tap). Then I set value in it and is being displayed as right aligned.

The thing is when I select the text field, suddenly the content moves to right side and displays as aligned right.

I change the layout to RTL and LTL programmatically with the following line:

UIView.appearance().semanticContentAttribute = .forceRightToLeft

I can't find a solution.

2

2 Answers

9
votes

Try this approach

 if isLangEn
    { 
        self.textF.textAlignment = NSTextAlignment.left  
        self.parentViewOfTextF.semanticContentAttribute = UISemanticContentAttribute.forceLeftToRight  
    }
    else
    {   
        self.textF.textAlignment = NSTextAlignment.right
        self.parentViewOfTextF.semanticContentAttribute = UISemanticContentAttribute.forceRightToLeft  
    }
1
votes

To set alignment of Label and Textfields text on changing language use the bellow line of code:

  1. To check app language:

    class Language {
    
      static var isArabicLanguage : Bool {
        get {
               return currentAppleLanguage() == "ar"
         }
       }
    }
    
  2. Create extension of UIViewController:

    extension UIViewController {
    
    //Align Textfield Text
    
    func loopThroughSubViewAndAlignTextfieldText(subviews: [UIView]) {
    if subviews.count > 0 {
        for subView in subviews {
            if subView is UITextField && subView.tag <= 0{
                let textField = subView as! UITextField
                textField.textAlignment = Language.isArabicLanguage ? .right: .left
            } else if subView is UITextView && subView.tag <= 0{
                let textView = subView as! UITextView
                textView.textAlignment = Language.isArabicLanguage ? .right: .left
    
            }
    
            loopThroughSubViewAndAlignTextfieldText(subviews: subView.subviews)
        }
      }
    }
    
    
    //Align Label Text
    func loopThroughSubViewAndAlignLabelText(subviews: [UIView]) {
    if subviews.count > 0 {
        for subView in subviews {
            if subView is UILabel && subView.tag <= 0 {
                let label = subView as! UILabel
                label.textAlignment = Language.isArabicLanguage ? .right : .left
            }
            loopThroughSubViewAndAlignLabelText(subviews: subView.subviews)
        }
       }
      }
    }
    
  3. Create new class Localizer which inherit from UIViewController which help us to set allignment of each viewController.

    class LocalizerViewController: UIViewController {
    
       override func viewDidLoad() {
          super.viewDidLoad()
    
          self.loopThroughSubViewAndAlignTextfieldText(subviews: self.view.subviews)
          self.loopThroughSubViewAndAlignLabelText(subviews: self.view.subviews)
      }
    }
    
  4. Now, Inherit all the ViewController to LocalizerViewController:

    class myViewController: LocalizerViewController {
           override func viewDidLoad() {
               super.viewDidLoad()
          }
    }
    

It will align all the label and textfields text to right or left according to your app language.