OGeek|极客世界-中国程序员成长平台

标题: ios - 应用本地化时 UITextField 占位符的颜色不同 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-11 18:15
标题: ios - 应用本地化时 UITextField 占位符的颜色不同

我有一个非常奇怪的问题。我有一个带有 IBInspectable var 的 UITextField 类来更改它的占位符颜色,我正在这样做:

attributedPlaceholder = NSAttributedString(string: placeholder != nil ?  placeholder! : "", attributes:[NSForegroundColorAttributeName : color])

当应用方案设置为System languageEnglish或任何LTR的语言时,它可以正常工作。

但当它设置为希伯来语或任何其他 RTL 语言时,它根本不会显示。 但是,在 Debug Hierarchy View 中显示 placeholder 在那里。

请注意,我使用 code 中的 Localizable.strings 而不是本地化 Storyboard来设置翻译。 除了 UITextfield 的占位符

外,一切正常

谢谢

enter image description here

编辑: 显然,所有发生的事情都是 attributedPlaceholder 在应用程序处于 LTR 模式时没有任何效果。 我将其中一个 UITextfields 背景更改为白色,它显示了。只有 attributedPlaceholderNSAttributedString 的颜色在 RTL 模式下不起作用

仍然需要一个解决方案

enter image description here



Best Answer-推荐答案


试试这个:

extension UITextField{

    override func setLocalized() -> Void {
        if UtilityClass.isLanguageRTL()// check RTL or LTR here {
            self.textAlignment = .right
            self.semanticContentAttribute = .forceRightToLeft
        }
        else{
            self.textAlignment = .left
            self.semanticContentAttribute = .forceLeftToRight
        }
    }

}

在 ViewWillAppear 中

    textField1?.setLocalized()
    textField2?.setLocalized()

关于ios - 应用本地化时 UITextField 占位符的颜色不同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45057861/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4