Rightpoint / BonMot

Beautiful, easy attributed strings in Swift
MIT License
3.57k stars 197 forks source link

UITextfield's placeholder color #315

Open amaurydavid opened 6 years ago

amaurydavid commented 6 years ago

In my UITextField, I want the placeholder and the text to use the same style. According to the doc, they both use the bonMotStyleproperty:

  • note: The style is applied to both the attributedText and attributedPlaceholder. If you plan on styling them differently, use attributed strings directly.

The issue here is that the style's color isn't correctly applied to the placeholder:

textField.bonMotStyle = StringStyle(.font(UIFont.systemFont(ofSize: 16)), .color(UIColor.red))

will result in a placeholder still being grey instead of red, but the text is correctly colored. Note: While the placeholder isn't colored, its font size is correctly set to 16 instead of 14.

However, a workaround can be to set the style to both bonMotStyle and attributedPlaceholder:

let style = StringStyle(.font(UIFont.systemFont(ofSize: 16)), .color(UIColor.red))
styledTextField.bonMotStyle = style
if let placeholder = styledTextField.placeholder {                              
     styledTextField.attributedPlaceholder = style.attributedString(from: placeholder)
}

Tested with BonMot 5.0 on iOS 11.2 and 10.3.1

pauluhn commented 6 years ago

I've got a fix but not sure the doc is accurate? It was last edited 2 years ago.

Should the placeholder text have the same style? From a HID perspective, it is conveying the wrong assumption that the placeholder is (text) data. Instead of adding code to match the doc, maybe update the doc to match the code?