根据当前语言更改文本字段的查看模式

时间:2018-07-29 05:54:37

标签: ios swift xcode

我尝试根据语言将编辑文本的图标从右向左更改。

我正在使用https://github.com/MoathOthman/MOLH进行本地化..

并且正在尝试这样做:

     override func viewDidAppear(_ animated: Bool) {


        let image2 = UIImage(named: "username")
        let border2 = CALayer()
        let width2 = CGFloat(1.0)
        let color2 = UIColor(red:0.80, green:0.80, blue:0.80, alpha:1.0)
        border2.borderColor = color2.cgColor
        border2.frame = CGRect(x: 0, y: self.email.frame.size.height - width2, width: self.email.frame.size.width, height: self.email.frame.size.height)
        border2.borderWidth = width2
        let paddingView2 = UIView(frame: CGRect(x: 0, y: 0, width: 25, height: 20))
        let paddingImage2 = UIImageView()
        paddingImage2.image = image2
        paddingImage2.contentMode = .scaleAspectFit
        paddingImage2.frame = CGRect(x: 5, y: 0, width: 20, height: 20)
        paddingView2.addSubview(paddingImage2)

        let image4 = UIImage(named: "password")
        let border4 = CALayer()
        let width4 = CGFloat(1.0)
        let color4 = UIColor(red:0.80, green:0.80, blue:0.80, alpha:1.0)
        border4.borderColor = color4.cgColor
        border4.frame = CGRect(x: 0, y: self.password.frame.size.height - width4, width: self.password.frame.size.width, height: self.password.frame.size.height)
        border4.borderWidth = width4
        let paddingView4 = UIView(frame: CGRect(x: 0, y: 0, width: 25, height: 20))
        let paddingImage4 = UIImageView()
        paddingImage4.image = image4
        paddingImage4.contentMode = .scaleAspectFit
        paddingImage4.frame = CGRect(x: 5, y: 0, width: 22, height: 17)
        paddingView4.addSubview(paddingImage4)

        if MOLHLanguage.isArabic(){
            print("arabic")
            self.email.rightViewMode = UITextFieldViewMode.always
            self.email.rightView = paddingView2
            self.password.rightViewMode = UITextFieldViewMode.always
            self.password.rightView = paddingView4
        }else{
            print("english")
            self.email.leftViewMode = UITextFieldViewMode.always
            self.email.leftView = paddingView2
            self.password.leftViewMode = UITextFieldViewMode.always
            self.password.leftView = paddingView4
        }

        self.email.layer.addSublayer(border2)
        self.email.layer.masksToBounds = true
        self.password.layer.addSublayer(border4)
        self.password.layer.masksToBounds = true

}

这将始终在文本字段的左侧显示图标。即使它已输入并正确打印当前语言,也可以用英语和阿拉伯语显示在文本字段的左视图上。

为什么会这样?以及如何解决?

0 个答案:

没有答案