如何在tvOS上的UITextField中更改占位符颜色,以使更改得以保留并且不会恢复为默认的占位符状态?

Tom*_*nko 3 uitextfield tvos

如何在tvOS上的UITextField中更改占位符颜色,以使更改得以保留并且不会恢复为默认的占位符状态?

在viewDidLoad()中,我添加了以下内容:

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)!])
Run Code Online (Sandbox Code Playgroud)

因此,起初占位符颜色为白色,但是当它失去焦点时,它将恢复为默认状态。

如何保持占位符颜色的变化?

小智 5

基本上,需要再次更新主线程中didUpdateFocus中的属性

 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])
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 不要忘记添加 super.didUpdateFocus(in: context, with: coordinator)` 以保留突出显示动画。 (2认同)