Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UITextfield text displayed as left aligned when the property is right in iOS?

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.

like image 274
Karan Alangat Avatar asked Dec 23 '22 11:12

Karan Alangat


1 Answers

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  
    }
like image 134
Sh_Khan Avatar answered May 16 '23 08:05

Sh_Khan