问题
How to change placeholder color in UITextField on tvOS, so that the change is kept and it does not revert back to its default placeholder state?
In viewDidLoad() I have added the following:
self.txfUsername?.attributedPlaceholder = NSAttributedString(string: "Email", attributes:[NSForegroundColorAttributeName: UIColor(red:1.0, green:1.0, blue:1.0, alpha:1.0), NSFontAttributeName:UIFont(name:"GothamBook", size:38.0)!])
So at first the placeholder color is white, however when it looses focus it reverts back to its default state.
How to keep the change of the placeholder color?
回答1:
Basically, need to update again the properties in didUpdateFocus in the main thread
override func didUpdateFocus(in context: UIFocusUpdateContext,
with coordinator: UIFocusAnimationCoordinator) {
DispatchQueue.main.async {
self.emailTextField.attributedPlaceholder = NSAttributedString(string: emailTitle,
attributes: [NSForegroundColorAttributeName:UIColor.white])
self.passwordTextField.attributedPlaceholder = NSAttributedString(string: passwordTitle,
attributes: [NSForegroundColorAttributeName:UIColor.white])
}
}
来源:https://stackoverflow.com/questions/37788135/how-to-change-placeholder-color-in-uitextfield-on-tvos-so-that-the-change-is-ke