えむじぃのアプリ開発

えむじぃのアプリ開発

元大手IT企業SE、現ベンチャー企業CTOのブログです。

【Swift】placeholderの色を変更

今回はUITextFieldのplaceholderの色を変更する方法をこの記事で説明します。

この記事のポイント・NSAttributedStringを使用する。

UIAlertControllerの宣言

UITextFieldを以下のように設定します。

let txtFld = UITextField()
txtFld.attributedPlaceholder = NSAttributedString(string: "半角英数字で入力してください。"
                , attributes: [NSAttributedStringKey.foregroundColor : UIColor.blue])

これでUITextFieldのplaceholderの色が変更されます。