Avatar ·

Comment changer la couleur du texte du placeholder dans UITextField avec Swift ?

Bonjour à tous ! Je suis débutant en développement iOS et je n'arrive pas à comprendre une tâche simple, à première vue.

\n\n

J'ai un UITextField avec un placeholder. La couleur grise standard ne correspond pas au design de mon application. J'ai besoin que le texte du placeholder soit, par exemple, bleu ou rouge.

\n\n

J'ai essayé de faire ceci :

\n
textField.placeholder = "Введите имя"\ntextField.placeholderColor = UIColor.blue // Cette propriété n'existe pas, Xcode génère une erreur
\n\n

Ensuite, j'ai trouvé sur Internet une méthode avec NSAttributedString :

\n
let placeholderText = NSAttributedString(string: "Введите имя", attributes: [NSAttributedString.Key.foregroundColor: UIColor.blue])\ntextField.attributedPlaceholder = placeholderText
\n\n

Cela a fonctionné