What's the default color for placeholder text

2020-02-16 06:29发布

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条回答
在下西门庆
2楼-- · 2020-02-16 07:08

Starting from iOS 13 you should use UIColor.placeholderText to make sure the element looks good in both light and dark modes. Documentation:

The color for placeholder text in controls or text views.

查看更多
Viruses.
3楼-- · 2020-02-16 07:11

You can get this colour from inspecting the attributedPlaceholder from the UITextField.

The default seems to be: NSColor = "UIExtendedSRGBColorSpace 0 0 0.0980392 0.22";

You could add an extension (or category) on UIColor:

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

2018, latest syntax is just:

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

#colorLiteralRed was deprecated. Be aware of this in some cases.

查看更多
叼着烟拽天下
4楼-- · 2020-02-16 07:11

I think you can get the color by use the tools in your Mac.enter image description here

查看更多
Evening l夕情丶
5楼-- · 2020-02-16 07:15

Better to grab the color off of a text field dynamically in case it changes in the future. Default to 70% gray, which is pretty close

extension UITextField {
  var placeholderColor: UIColor {
    return attributedPlaceholder?.attributes(at: 0, effectiveRange: nil)[.foregroundColor] as? UIColor ?? UIColor(white: 0.7, alpha: 1)
  }
}
查看更多
Summer. ? 凉城
6楼-- · 2020-02-16 07:17

Set label font to "light"
mylabel.font = [UIFont fontWithName:@"HelveticaNeue-Light" size:14.0f];
and color code for placeholder text is
#c2b098

查看更多
相关推荐>>
7楼-- · 2020-02-16 07:18

According to the Apple code, it is 70% gray

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

and if we convert it to rgb :

UIColor.init(red: 178/255, green: 178/255, blue: 178/255, alpha: 1)
查看更多
登录 后发表回答