lukeredpath / swift-responsive-textfield

A SwiftUI wrapper around UITextField with binding-based state and responder control
Apache License 2.0
85 stars 15 forks source link

Placeholder Text Colour #14

Closed chriskilpin closed 1 year ago

chriskilpin commented 1 year ago

Hi Luke. Thanks for a great library. Was wondering if it is possible to change the colour of the placeholder text?

lukeredpath commented 1 year ago

If you use the configuration system to get direct access to the underlying UITextField you can set an attributedPlaceholder instead, which is the only way you can change the placeholder styling as far as I'm aware.

chriskilpin commented 1 year ago

I wasn’t aware you could get access to the underlying textfield, I will give that a try. Thanks for getting back to me.

chriskilpin commented 1 year ago

Thanks that worked!

mergesort commented 8 months ago

I don't want to reopen this issue but wanted to ask a clarifying question. When I do something like this, the placeholder string is blank.

$0.attributedPlaceholder = NSAttributedString(
    string: "Placeholder",
    attributes: [NSAttributedString.Key.foregroundColor: UIColor.gray]
)

Is the right use of this library to set the placeholder text and use the attributedPlaceholder for applying the color, or would you normally expect just setting the attributedPlaceholder to work?

lukeredpath commented 8 months ago

@mergesort I would expect setting the attributed placeholder to work, I'm not sure why it wouldn't. 😕

mergesort commented 8 months ago

Sorry, just dug in a little bit more and realized that this is likely because ResponsiveTextField initializer has a mandatory parameter of placeholder which I've defaulted to "". Though that's my implementation, I don't see a way to have an overriding attributedPlaceholder, since the library will use whatever placeholder you pass in as a parameter.

lukeredpath commented 8 months ago

Ah, in that case it might need a change to make the placeholder parameter an optional string and only set it if it is non-nil, which should allow you to set the attributed placeholder instead. I'd accept a PR :)

mergesort commented 8 months ago

Duly noted, will try to sneak that in for ya!

mergesort commented 8 months ago

Hmm, I thought marking placeholder as optional would do the trick since UITextField.placeholder is actually String? out the box but I'm still not seeing an attributedPlaceholder being drawn after this commit.

lukeredpath commented 8 months ago

Hmm, according to the docs it might not actually be possible to change the placeholder colour anyway: https://developer.apple.com/documentation/uikit/uitextfield/1619610-attributedplaceholder

This property is nil by default. If set, the placeholder string is drawn using system-defined color and the remaining style information (except the text color) of the attributed string

mergesort commented 8 months ago

So strange. I find myself able to change the placeholder color by changing both properties, but maybe it's a hack that this works when done in tandem? Screenshot 2024-02-27 at 10 25 27

mergesort commented 8 months ago

I'll put this into a pull request but I've gone ahead and added a new responsiveTextFieldPlaceholderColor which satisfies my need of being able to update the placeholder text color.

Screenshot 2024-02-27 at 10 49 11

It may not be exactly the approach you're looking for so I'm happy to make changes for more general purposes, but it seemed like a good idea if the main purpose is to update the placeholder text, and in line with the APIs you've exposed.