5

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.

Eric Aya
  • 69,473
  • 35
  • 181
  • 253
Karan Alangat
  • 2,154
  • 4
  • 25
  • 56

3 Answers3

10

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  
    }
Shehata Gamal
  • 98,760
  • 8
  • 65
  • 87
  • Thanks for your reply. I sorted it out by myself. I ve been setting value first then setting alignment and layout during loading view. I changed the order in such a way that layout first and then set value. And it worked :) – Karan Alangat Jan 03 '18 at 12:41
1

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.

Umair Ali
  • 758
  • 8
  • 17
0

In Xcode 14.1 (or less perhaps) there is a setting "Natural" in text alignment which solved problem for me.

Vadim
  • 3,855
  • 2
  • 17
  • 22