How Can I set Localised Direction within application?(RTL if user select arabic, LTR is selected language is English)

前端 未结 4 662

My Application must support Arabic(right to left direction) and English(left to right direction) language, I need to set UI and based on user select language from my applica

4条回答
  •  误落风尘
    2020-12-07 18:51

    Use this line of code it will do your magic and will change layout without closing application. From right to left

    UIView.appearance().semanticContentAttribute = .forceRightToLeft

    And for Right to Left Flip

    UIView.appearance().semanticContentAttribute = .forceLeftToRight
    

    and if you want to change textfield layout or text change then use this code because i faced this issue . textfield's texts was not changning layout. check this code to change layout of textfield text.

    extension UITextField {
      open override func awakeFromNib() {
        super.awakeFromNib()
        if UserDefaults.languageCode == "ar" {
            if textAlignment == .natural {
                self.textAlignment = .right
            }
        }
    }
    }
    

提交回复
热议问题