Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Placeholder Text Colour #14

Closed
chriskilpin opened this issue Apr 6, 2023 · 12 comments
Closed

Placeholder Text Colour #14

chriskilpin opened this issue Apr 6, 2023 · 12 comments

Comments

@chriskilpin
Copy link

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

@lukeredpath
Copy link
Owner

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
Copy link
Author

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
Copy link
Author

Thanks that worked!

@mergesort
Copy link
Contributor

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
Copy link
Owner

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

@mergesort
Copy link
Contributor

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
Copy link
Owner

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
Copy link
Contributor

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

@mergesort
Copy link
Contributor

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
Copy link
Owner

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
Copy link
Contributor

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
Copy link
Contributor

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants