[SWIFT] I thought about an extension that can select the color of placeHolder in one shot with UITextFiled

Introduction

Thank Hello, this is TOSH. When using UITextField, there are many people who feel that textColor can be set as it is, but for some reason the color of placeHolder cannot be set as it is. So, I thought about an extension that can be set for placeHolder in the same way as TextColor.

Normal method

Normally, if you want to change the color of placeHolder,

let color: UIColor = ~Specify any color here~
textField.attributedPlaceholder = NSAttributedString(
                string: "Characters in placeHolder",
                attributes: [NSAttributedString.Key.foregroundColor : color])

Well, it's not that hard, but it's like using NSAttibutedString.

Implementation method using Extension

extension UITextField {
    var placeHolderColor: UIColor {
        set {
            self.attributedPlaceholder = NSAttributedString(
                string: self.placeholder ?? "",
                attributes: [NSAttributedString.Key.foregroundColor : newValue])
        }
        get {
            let defaultPlaceHolderGray = UIColor(red: 0, green: 0, blue: 0.0980392, alpha: 0.22)
            guard self.attributedPlaceholder?.length != 0,
                  let placeHolderColor =  self.attributedPlaceholder?.attribute(.foregroundColor, at: 0, effectiveRange: nil) as? UIColor else {
                return defaultPlaceHolderGray
            }
            return placeHolderColor
        }
    }
}

Stored Property cannot be used in extension, so Computed Property is used. The setter is easy, but the one that struggled unexpectedly was the getter. It is possible that there are no characters in the Place Holder, so you need to guard with self.attributedPlaceholder? .Length! = 0. Also, since it only takes the first character of attributedPlaceholder, if you encounter an AttributedString whose character changes after the first character, you can think that it is not working well. However, even if it is set, it seems that it will not be fetched easily, so I feel that I do not have to think too much about this. .. ..

Summary

What I noticed while implementing it is that I set the color of Place Holder in the first place, but it does not mean that I do not set the contents, so I am using NSAttributedString which can be done at the same time as setting text. thought. (In the case of Text, the user inputs characters, so the main usage is to specify the color in the initial state and not specify Text) So it's okay to make it, but it feels like it's an extension that isn't often used.

Recommended Posts

I thought about an extension that can select the color of placeHolder in one shot with UITextFiled
I thought about the strategy of introducing Combine in iOS development
[Swift] I tried using ColorPicker (the one that can easily select colors) added in iOS14 [Swift UI]
The strongest Omikuji app that only I, an EKS Lover, can think of
I checked the number of taxis with Ruby
I investigated the super that I met with the devise controller
Why can I use the rails command installed with gem? ??
Four-in-a-row with gravity that can be played on the console
I tried using the CameraX library with Android Java Fragment
Can I try all combinations with an app that has 20 checkboxes?
I checked because the response was strange when debugging with Tomcat 8
[Java] I want to perform distinct with the key in the object
I thought about an extension that can select the color of placeHolder in one shot with UITextFiled
When I think about the 402 error that suddenly appeared in the middle of the introduction of PAY.jp, there was an unexpected place
I made an interpreter (compiler?) With about 80 lines in Ruby.
[Java] I thought about the merits and uses of "interface"
The first year of new graduates thought about an implementation method that makes use of TDD (ruby)
How to deal with the type that I thought about writing a Java program for 2 years
Can I try all combinations with an app that has 20 checkboxes?
Command line that can create a directory structure for building a Laravel environment with Docker in one shot
About the problem that the server can not be started with rails s
I thought about the best way to create a ValueObject in Ruby