textfield.placeholderColor
みたいな感じで簡単に変更できるかなと思っていたのですが、そういう訳には行かなかったです。
開発環境 | バージョン |
---|---|
Xcode | 12.1 |
iOS | 14.1 |
Swift | 5 |
textFieldのattributedPladeholderに値を代入してスタイルを変更する
textField.attributedPlaceholder = NSAttributedString(string: "placeholder text",
attributes: [NSAttributedString.Key.foregroundColor: UIColor.red])
foegroundColorで色を変更する
attributes: [NSAttributedString.Key.foregroundColor: UIColor.red]
今回は例で赤色にしている箇所をお好みの色に変更すればplaceholderの色が変更出来ます。
おまけ
・attributed
って何だと思い意味を調べたら、
〜に帰属する
という意味でした。
・attributedPladeholder
の説明には、このように記載があります。
The styled string that is displayed when there is no other text in the text field.
テキストフィールドにテキストが無い時に表示されるスタイルされたStringです
とのことです。
そのスタイルにNSAttributedString
を代入して決めてあげる感じみたいです。