4
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

【Swift】UITextFieldのplaceholderの色を変更する

Last updated at Posted at 2020-11-19

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を代入して決めてあげる感じみたいです。

参考

SwiftでPlaceholderテキストの色を変更する

4
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?