使用Swift更改占位符文本颜色 [英] Changing Placeholder Text Color with Swift

查看:341
本文介绍了使用Swift更改占位符文本颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的设计实现了深蓝色 UITextField ,因为占位符文本默认为深灰色,我几乎无法确定占位符文本所说的内容。

I have a design that implements a dark blue UITextField, as the placeholder text is by default a dark grey colour I can barely make out what the place holder text says.

我已经搜索了问题,但我还没有提出使用Swift语言而不是Obj-c的解决方案。

I've googled the problem of course but I have yet to come up with a solution while using the Swift language and not Obj-c.

有没有办法使用Swift更改 UITextField 中的占位符文本颜色?

Is there a way to change the placeholder text colour in a UITextField using Swift?

推荐答案

您可以使用属性字符串设置占位符文本。使用属性传递所需的颜色

You can set the placeholder text using an attributed string. Pass the color you want with the attributes:

var myTextField = UITextField(frame: CGRect(x: 0, y: 0, width: 200, height: 30))
myTextField.backgroundColor = .blue
myTextField.attributedPlaceholder = NSAttributedString(string: "placeholder text",
                             attributes: [NSForegroundColorAttributeName: UIColor.yellow])

对于Swift 3+使用以下:

myTextField.attributedPlaceholder = NSAttributedString(string: "placeholder text",
                             attributes: [NSAttributedStringKey.foregroundColor: UIColor.white])

这篇关于使用Swift更改占位符文本颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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