ios UITextField 中占位符文本的默认颜色是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31057746/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What's the default color for placeholder text in UITextField?
提问by pterry26
Does anyone know what color a UITextField
's placeholder text is, by default? I'm trying to set a UITextView
's text to the same color. I've read elsewhere that it is UIColor.lightGrayColor()
but it is actually a little lighter.
有谁知道UITextField
默认情况下a的占位符文本是什么颜色?我正在尝试将 aUITextView
的文本设置为相同的颜色。我在别处读到它是,UIColor.lightGrayColor()
但实际上它更轻一些。
采纳答案by Bram De Geyter
You can get this colour from inspecting the attributedPlaceholder
from the UITextField
.
您可以从检查得到这个颜色attributedPlaceholder
从UITextField
。
The default seems to be: NSColor = "UIExtendedSRGBColorSpace 0 0 0.0980392 0.22";
默认似乎是: NSColor = "UIExtendedSRGBColorSpace 0 0 0.0980392 0.22";
You could add an extension (or category) on UIColor
:
您可以在以下位置添加扩展名(或类别)UIColor
:
extension UIColor {
static var placeholderGray: UIColor {
return UIColor(colorLiteralRed: 0, green: 0, blue: 0.0980392, alpha: 0.22)
}
}
2018, latest syntax is just:
2018 年,最新的语法是:
extension UIColor {
static var officialApplePlaceholderGray: UIColor {
return UIColor(red: 0, green: 0, blue: 0.0980392, alpha: 0.22)
}
}
#colorLiteralRed
was deprecated. Be aware of thisin some cases.
#colorLiteralRed
已弃用。在某些情况下要注意这一点。
回答by Daniel Fernandes
The colour is #C7C7CD(r: 199 g:199 b: 205) (as what pterry26 said)
颜色是#C7C7CD(r: 199 g:199 b: 205) (正如pterry26所说)
and the font-family is HelveticaNeue-Mediumand size is 16
字体系列是HelveticaNeue-Medium,大小是16
Note that this is a guess at what the color looks like on a screen. For the actual values, simply inspect the Apple code for attributedPlaceholder
.
请注意,这是对屏幕上颜色的猜测。对于实际值,只需检查 Apple 代码中的attributedPlaceholder
.
回答by pterry26
I sent a screenshot to my mac and used Photoshop's eyedropper tool. For anyone interested, this is at least a very good approximation of the placeholder color on a white background:
我将屏幕截图发送到我的 mac 并使用了 Photoshop 的吸管工具。对于任何感兴趣的人,这至少是白色背景上占位符颜色的一个非常好的近似值:
Red: 199, Green: 199, Blue: 205
红色:199,绿色:199,蓝色:205
回答by Peter Staev
The actual color is not a solid one but has transparency in it. So the closest color is
实际颜色不是纯色,而是具有透明度。所以最接近的颜色是
Red: 4, Green: 4, Blue: 30, Alpha: ~22%
红色:4,绿色:4,蓝色:30,Alpha:~22%
If you use this with a white background you will get what @pterry26 wrote above.
如果您在白色背景下使用它,您将得到上面@pterry26 所写的内容。
回答by DogCoffee
Using the values from the correct answer above
使用上面正确答案中的值
extension UIColor {
class func greyPlaceholderColor() -> UIColor {
return UIColor(red: 0.78, green: 0.78, blue: 0.80, alpha: 1.0)
}
}
回答by AbdulAziz Rustam Ogli
According to the Apple code, it is 70% gray
根据苹果代码,它是 70% 灰色
open var placeholder: String? // default is nil. string is drawn 70% gray
and if we convert it to rgb :
如果我们将其转换为 rgb :
UIColor.init(red: 178/255, green: 178/255, blue: 178/255, alpha: 1)
回答by danqing
Just to add that in iOS 13 (and later), the placeholder color is exposed by Apple via
只是在 iOS 13(及更高版本)中补充一点,占位符颜色由 Apple 通过
UIColor.placeholderText
and it's dynamic (supports both dark and light).
并且它是动态的(支持黑暗和光明)。
Putting it with pre-iOS 13:
将其与 iOS 13 之前的版本一起使用:
static var placeholderText: UIColor {
if #available(iOS 13.0, *) {
return .placeholderText
}
return UIColor(red: 60, green: 60, blue: 67)!.withAlphaComponent(0.3)
}
回答by Andrey Gordeev
Starting from iOS 13you should use UIColor.placeholderText
to make sure the element looks good in both light and dark modes. Documentation:
从iOS 13开始,您应该使用它UIColor.placeholderText
来确保元素在明暗模式下看起来都不错。文档:
The color for placeholder text in controls or text views.
控件或文本视图中占位符文本的颜色。
回答by RyanM
Better to grab the color off of a text field dynamically in case it changes in the future. Default to 70% gray, which is prettyclose
最好动态地从文本字段中获取颜色,以防将来发生变化。默认为 70% 灰色,非常接近
extension UITextField {
var placeholderColor: UIColor {
return attributedPlaceholder?.attributes(at: 0, effectiveRange: nil)[.foregroundColor] as? UIColor ?? UIColor(white: 0.7, alpha: 1)
}
}
回答by Vaibhav Saran
I believe it is R:191 G:191 B:198 A:1
. See image below. Here marked controls are UIButton
with above Title TextColor
and rest are UITextFields
with default placeholder color. If iOS makes difference, then this one is iOS 9.
我相信是的R:191 G:191 B:198 A:1
。见下图。这里标记的控件是UIButton
上面的Title TextColor
,其余的是UITextFields
默认的占位符颜色。如果 iOS 有所作为,那么这就是 iOS 9。