[英]UITextField placeholder is in different color when app is localized
I have a really weird problem.我有一个非常奇怪的问题。 I have a
UITextField
class with IBInspectable
var to change it's placeholder color and I'm doing it like this:我有一个带有
IBInspectable
var 的UITextField
类来更改它的占位符颜色,我这样做是这样的:
attributedPlaceholder = NSAttributedString(string: placeholder != nil ? placeholder! : "", attributes:[NSForegroundColorAttributeName : color])
When the app scheme is set to System language
, English
or any language which is LTR
, it works fine.当应用方案设置为
System language
、 English
或任何LTR
语言时,它工作正常。
But when it's set to Hebrew or any other language which is RTL
, it doesn't show at all.但是当它设置为希伯来语或任何其他
RTL
语言时,它根本不显示。 However, in the Debug Hierarchy View
it shows that the placeholder
is there.但是,在
Debug Hierarchy View
中它显示placeholder
在那里。
Note that I am setting the translations using the Localizable.strings
in the code
instead of the localized storyboards.请注意,我在
code
中使用Localizable.strings
而不是本地化的故事板来设置翻译。 Everything works except for UITextfield
s placeholders除了
UITextfield
的占位符外,一切正常
Thanks:)谢谢:)
EDIT: Apparently, all that happens is that attributedPlaceholder
is not taking any effect whatsoever when the app is in LTR mode.编辑:显然,所有发生的事情是当应用程序处于 LTR 模式时,
attributedPlaceholder
没有任何效果。 I changed one of the UITextfields background to white, and it shows.我将其中一个 UITextfields 背景更改为白色,它显示了。 Only the
attributedPlaceholder
& NSAttributedString
s color doesn't work on RTL mode只有
attributedPlaceholder
和NSAttributedString
的颜色在 RTL 模式下不起作用
Still need a solution though:(仍然需要一个解决方案:(
Try this:试试这个:
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
}
}
}
In ViewWillAppear在视图中会出现
textField1?.setLocalized()
textField2?.setLocalized()
I also occurred the same situation that placeholder fonts were changed after localization (Not only LTR setting).我也发生了同样的情况,占位符字体在本地化后被更改(不仅是 LTR 设置)。
We had a customized object inherit from UITextField
and change the color when initialize.我们有一个自定义对象继承自
UITextField
并在初始化时更改颜色。
@IBInspectable
var placeholderColor: UIColor = UIColor.white {
didSet {
attributedPlaceholder = NSAttributedString(string: placeholder ?? "", attributes: [NSAttributedString.Key.foregroundColor: placeholderColor])
}
}
override init(frame: CGRect) {
super.init(frame: frame)
....
attributedPlaceholder = NSAttributedString(string: placeholder ?? "", attributes: [NSAttributedString.Key.foregroundColor: placeholderColor])
....
}
As using the Localizable.strings
, we translated the string in ViewController
as below.在使用
Localizable.strings
时,我们将ViewController
中的字符串翻译如下。
emailTextField.placeholder = "Generic.Textfield.Email".localized
However, when changing the string of placeholder didn't trigger to change placeholder color.但是,更改占位符字符串时不会触发更改占位符颜色。 Thus, it turns to the default value (R0 G0 B 0.1 A0.22).
因此,它变为默认值(R0 G0 B 0.1 A0.22)。
We override placeholder
manually and modify the attributedPlaceholder as the way as we did in the initialization.我们手动
override placeholder
并按照我们在初始化中所做的方式修改 attributedPlaceholder。 And FIX: Hope it can help others as well :)和 FIX:希望它也能帮助其他人 :)
PS: Seems Apple fix after iOS 13 but it works for the version before iOS 12. PS:似乎 Apple 在 iOS 13 之后进行了修复,但它适用于 iOS 12 之前的版本。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.