如何在 tvOS 上的 UITextField 中更改占位符颜色,以便保留更改并且不会恢复到其默认占位符状态? [英] 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?

查看:23
本文介绍了如何在 tvOS 上的 UITextField 中更改占位符颜色,以便保留更改并且不会恢复到其默认占位符状态?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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?

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

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?

推荐答案

基本上需要在主线程重新更新didUpdateFocus中的属性

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])
    }
}

这篇关于如何在 tvOS 上的 UITextField 中更改占位符颜色,以便保留更改并且不会恢复到其默认占位符状态?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆