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

时间:2015-06-25 18:12:48

标签: ios uitextfield uicolor

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.

13 个答案:

答案 0 :(得分:99)

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

且字体系列 HelveticaNeue-Medium 且尺寸 16

请注意,这是猜测屏幕上的颜色。对于实际值,只需检查Apple代码attributedPlaceholder

答案 1 :(得分:29)

您可以通过attributedPlaceholder检查UITextField来获取此颜色。

默认似乎是: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已被弃用。在某些情况下要注意this

答案 2 :(得分:11)

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: Red: 199, Green: 199, Blue: 205

答案 3 :(得分:10)

实际颜色不是实心颜色,但具有透明度。所以最接近的颜色是

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

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

答案 4 :(得分:6)

使用上面正确答案中的值

extension UIColor {

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

}

答案 5 :(得分:2)

根据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)

答案 6 :(得分:2)

只需添加一下,在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)
}

答案 7 :(得分:0)

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

答案 8 :(得分:0)

代码#999999完全符合我的表格!

答案 9 :(得分:0)

更好地从文本字段中动态获取颜色,以防将来更改。默认为70%灰色,即 pretty close

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

答案 10 :(得分:0)

iOS 13 开始,您应使用<button type="submit" height="40px" color="white" class="Button1"><span>Let's GO</span></button> 来确保该元素在亮和暗模式下都看起来不错。 Documentation:

  

控件或文本视图中占位符文本的颜色。

答案 11 :(得分:-2)

我认为您可以使用Mac中的工具获取颜色。enter image description here

答案 12 :(得分:-2)

将标签字体设置为&#34;灯&#34;
mylabel.font = [UIFont fontWithName:@&#34; HelveticaNeue-Light&#34;尺寸:14.0f];
占位符文本的颜色和颜色代码为#c2b098

相关问题