UITextField 中占位符文本的默认颜色是什么?

What's the default color for placeholder text in UITextField?

有谁知道 UITextField 的占位符文本默认是什么颜色?我正在尝试将 UITextView 的文本设置为相同的颜色。我在别处读到它是 UIColor.lightGrayColor() 但它实际上更轻一些。

我向 mac 发送了一张截图并使用了 Photoshop 的吸管工具。对于任何感兴趣的人,这至少是白色背景上占位符颜色的非常好的近似值:

红色:199, 绿色:199, 蓝色:205

颜色是#C7C7CD (r: 199 g:199 b: 205) (如pterry26所说)

字体系列为 HelveticaNeue-Medium 大小为 16


请注意,这是对颜色在屏幕上的外观的猜测。对于实际值,只需检查 attributedPlaceholder.

的 Apple 代码

我认为您可以使用 Mac 中的工具获得颜色。

将标签字体设置为 "light"
mylabel.font = [UIFont fontWithName:@"HelveticaNeue-Light" size:14.0f];
占位符文本的颜色代码是
#c2b098

使用上述正确答案的值

extension UIColor {

    class func greyPlaceholderColor() -> UIColor {
        return UIColor(red: 0.78, green: 0.78, blue: 0.80, alpha: 1.0)
    }

}

我相信是R:191 G:191 B:198 A:1。见下图。这里标记的控件是 UIButton,上面 Title TextColor,其余的是 UITextFields,默认占位符颜色。如果 iOS 有所不同,那么这个就是 iOS 9。

实际颜色不是纯色,而是透明的。所以最接近的颜色是

红色:4,绿色:4,蓝色:30,Alpha:~22%

如果你在白色背景下使用它,你会得到@pterry26 上面写的内容。

代码 #999999 与我的表单完全匹配!

您可以通过检查 UITextField 中的 attributedPlaceholder 获得此颜色。

默认好像是:NSColor = "UIExtendedSRGBColorSpace 0 0 0.0980392 0.22";

您可以在 UIColor 上添加扩展(或类别):

extension UIColor {
    static var placeholderGray: UIColor {
        return UIColor(colorLiteralRed: 0, green: 0, blue: 0.0980392, alpha: 0.22)
    }
}

2018,最新语法为:

extension UIColor {
    static var officialApplePlaceholderGray: UIColor {
        return UIColor(red: 0, green: 0, blue: 0.0980392, alpha: 0.22)
    }
}

#colorLiteralRed 已弃用。在某些情况下请注意

根据 Apple 代码,它是 70% 灰色

open var placeholder: String? // default is nil. string is drawn 70% gray

如果我们将其转换为 rgb :

UIColor.init(red: 178/255, green: 178/255, blue: 178/255, alpha: 1)

最好动态抓取文本字段的颜色,以防将来发生变化。默认为 70% 灰色,即 漂亮 关闭

extension UITextField {
  var placeholderColor: UIColor {
    return attributedPlaceholder?.attributes(at: 0, effectiveRange: nil)[.foregroundColor] as? UIColor ?? UIColor(white: 0.7, alpha: 1)
  }
}

只是补充一点,在 iOS 13(及更高版本)中,占位符颜色由 Apple 通过

公开
UIColor.placeholderText

而且它是动态的(支持暗色和亮色)。

把它放在前面iOS 13:

static var placeholderText: UIColor {
  if #available(iOS 13.0, *) {
    return .placeholderText
  }
  return UIColor(red: 60, green: 60, blue: 67)!.withAlphaComponent(0.3)
}

iOS 13 开始,您应该使用 UIColor.placeholderText 来确保元素在明暗模式下看起来都不错。 Documentation:

The color for placeholder text in controls or text views.