菜鸟教程小白 发表于 2022-12-11 18:15:38

ios - 应用本地化时 UITextField 占位符的颜色不同


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

<pre><code>attributedPlaceholder = NSAttributedString(string: placeholder != nil ?placeholder! : &#34;&#34;, attributes:)
</code></pre>

<p>当应用方案设置为<code>System language</code>、<code>English</code>或任何<code>LTR</code>的语言时,它可以正常工作。</p>

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

<p>请注意,我使用 <code>code</code> 中的 <code>Localizable.strings</code> 而不是本地化 Storyboard来设置翻译。
除了 <code>UITextfield</code> 的占位符</p> 外,一切正常

<p>谢谢:)</p>

<p> <a href="/image/sPcel.jpg" rel="noreferrer noopener nofollow"><img src="/image/sPcel.jpg" alt="enter image description here"/></a> </p>

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

<p>仍然需要一个解决方案:(</p>

<p> <a href="/image/EQ4pe.png" rel="noreferrer noopener nofollow"><img src="/image/EQ4pe.png" alt="enter image description here"/></a> </p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>试试这个:</p>

<pre><code>extension UITextField{

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

}
</code></pre>

<p><strong>在 ViewWillAppear 中</strong></p>

<pre><code>    textField1?.setLocalized()
    textField2?.setLocalized()
</code></pre></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 应用本地化时 UITextField 占位符的颜色不同,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/45057861/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/45057861/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 应用本地化时 UITextField 占位符的颜色不同